[LeetCode] Reverse Bits
2015-07-13 19:37
281 查看
Question:
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?
1、题型分类:
2、思路:
3、时间复杂度:
4、代码:
注意加粗部分,先移位还是先或的区别!
5、优化:
6、扩展:
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?
1、题型分类:
2、思路:
3、时间复杂度:
4、代码:
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int i=0,result=0; while(i<32) { result=(result<<1)|(n&1); n=n>>1; i++; } return result; } }
注意加粗部分,先移位还是先或的区别!
5、优化:
6、扩展:
相关文章推荐
- Jquery插件开发
- 通过配置worker_cpu_affinity参数提升nginx性能
- c# 中LinkLabel捕获F2进行重命名
- 程序员必知的8大排序(四)-------归并排序,基数排序(java实现)
- 但从谈论性能点SQL Server选择聚集索引键
- 单源最短路径(Dijkstra)
- HDU 1754 I Hate It
- Linux 技巧:让进程在后台可靠运行的几种方法
- poj2559 Largest Rectangle in a Histogram
- 海洋时代(葡萄牙)
- 20分钟打造你的Bootstrap站点
- Rabbitmq-Java-Client重连和consumer恢复机制
- hdu 2539 点球大战
- QLineEdit 输入验证(相关的设置)
- POJ 1056 IMMEDIATE DECODABILITY
- 9部美到不行的爱情故事
- 【转】Hadoop 1.x中fsimage和edits合并实现
- Java-BlockingQueue的使用
- POJ——1458最长公共子序列
- rapidJson 的使用