190. Reverse Bits
2016-07-19 15:44
351 查看
190. Reverse Bits
Leetcode link for this questionDiscription:
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Analyze:
Code 1:
class Solution(object): def reverseBits(self, n): """ :type n: int :rtype: int """ bi=str(bin(n))[2:] bi='0'*(32-len(bi))+bi s=0 wei=1 for i in bi: s=int(i)*wei+s wei*=2 return s
Submission Result:
Status: AcceptedRuntime: 68 ms
Ranking: beats 31.14%
Code 2:
class Solution(object): def reverseBits(self, n): """ :type n: int :rtype: int """ for i in range(16): j=32-i-1 if i>j: break bi=(n>>i)&1 bj=(n>>j)&1 if bi^bj: n=n^(1<<i)^(1<<j) return n
Submission Result:
Status: AcceptedRuntime: 72 ms
Ranking: beats 21.27%
相关文章推荐
- 报表或BI的价值在哪?
- 动态加载JDBC Jar包连接数据库
- 通过按钮复制一段代码
- Machine Learning week3
- hdu 2577
- android 跑马灯
- 使用Devstack部署neutron网络节点
- 自定义Dialog且实现与Activity交互
- 源码分析-java-AbstractList-subList、RandomAccess、equals和hashCode
- 向 Activity 添加片段
- 关于row_number函数的用法
- 9. Palindrome Number
- C++右值引用
- HUST 1599 Multiple
- [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
- javascript获取dom的下一个节点方法
- PAT乙级.1018. 锤子剪刀布 (20)
- Javascript基础知识
- MarkDown技巧:两种方式实现页内跳转
- 【Heritrix 03】heritrix_dmesg.log