leetcode reverse bits python
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
python代码:
from string import atoi
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
n_str=bin(n)[2:].zfill(32)[-1::-1] #利用bin内建函数将n转换为二进制字符串,利用切片删掉前缀0b,并将其扩充至32位,然后在反转
n_int=string.atoi(n_str,2) #将翻转后的二进制字符串转换为整形
return n_int
leetcode reverse bits python的更多相关文章
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [leetcode] Reverse Bits
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
- LeetCode Reverse Bits 反置位值
题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...
- [LeetCode] Reverse Bits 位操作
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- leetcode Reverse Integer python
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...
- lc面试准备:Reverse Bits
1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...
- 【LeetCode】190. Reverse Bits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- update openssl on redhat/centos
$ openssl versionOpenSSL 1.0.1e-fips 11 Feb 2013 $ yum list |grep opensslopenssl.x86_64 1.0.1e-16.el ...
- android学习笔记40——国际化和资源自适应
国际化——Internationalization,简称I18N. 本地化——Localization,检查L10N. java国际化资源的思路: java提供国际化资源的思路,是将程序中的标签.提示 ...
- android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)
DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...
- Nginx实现内参:为什么架构很重要?
Nginx在web开发者眼中就是高并发高性能的代名词,其基于事件的架构也被众多开发者效仿.我从Nginx的网站找到一篇技术文章将Nginx是怎样实现的,文章是Nginx的产品老大Owen Garret ...
- Visual C++ for Linux Development
原文 https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/ Visual C++ fo ...
- linux内核模块相关命令:lsmod,depmod,modprobe,modinfo,insmod,rmmod 使用说明
加载内核驱动的通常流程: 1.先将.ko文件拷贝到/lib/module/`uname -r`(内核版本号)/kernel/driver/...目录下, 根据具体用途的区别分为net.ide.scsi ...
- 黄聪:VS2010开发T4模版引擎之基础入门
原文:http://www.cnblogs.com/lzrabbit/archive/2012/07/15/2591085.html 额,T4好陌生的名字,和NuGet一样很悲催,不为世人所熟知,却又 ...
- 《黄聪:手机移动站SEO优化教程》4、如何实现手机移动网站和PC站点的自主适配
视频地址:http://www.tudou.com/programs/view/v4Hur5vjav4/ 1.自主适配 A:站点自己做好PC与手机之间的适配,以及手机站各个版式之间的适配.当手机用户通 ...
- 把centos 的mysql 重装一下 把原来的lnmp删除,怎么备份还原数据库
mysqldump --lock-all-tables -u root -p --databases mydb > /opt/database/mydb.sql,或者直接备份mysql的数据存储 ...
- ubuntu 关机,重启,注销命令
1关机命令 shutdown 好像ubuntu的终端中默认的是当前用户的命令,只是普通用户,因此在终端器中可以使用sudo -sh 转换到管理员root用户下执行命令. 1)shutdown –hel ...