leetcode:190 Reverse Bits-每日编程第三十二题
2015-12-24 16:03
387 查看
Reverse Bits
Total
Accepted: 46760 Total
Submissions: 160246 Difficulty: Easy
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?
思路:
分块交换,对于8位数,abcdefgh->efghabcd->ghefcdab->hgfedcba。对于32位,同理。
Total
Accepted: 46760 Total
Submissions: 160246 Difficulty: Easy
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?
思路:
分块交换,对于8位数,abcdefgh->efghabcd->ghefcdab->hgfedcba。对于32位,同理。
<span style="font-size:14px;">class Solution { public: uint32_t reverseBits(uint32_t n) { n=(n<<16)|(n>>16); n=(((n&0x00ff00ff)<<8)|((n&0xff00ff00)>>8)); n=(((n&0xf0f0f0f0)>>4)|((n&0x0f0f0f0f)<<4)); n=(((n&0x33333333)<<2)|((n&0xcccccccc)>>2)); n=(((n&0x55555555)<<1)|((n&0xaaaaaaaa)>>1)); return n; } };</span>
相关文章推荐
- eclipse, Log4j配置(真心的详细~)
- Java设计模式之单例模式
- (ros/qt报错) FATAL: ROS_MASTER_URI is not defined in the environment
- C#客户端的异步操作
- comet4j-java服务器推送实例
- asp.net mvc杂项收集
- Java工程师成神之路
- <Java> File文件操作
- 避开WebForm天坑,拥抱ASP.Net MVC吧
- leetcode:88 Merge Sorted Array-每日编程第三十一题
- winpython好用到哭了,自带numpy,scipy,matplotlib,scikit-learn
- 通过PHP自带的服务器来查看正则匹配结果的方法
- C++中inline函数
- Matlab中函数句柄@的作用及介绍
- (原+转)C++中的lambda表达式
- Java多线程和并发性知识点总结
- 有一个fuck叫做wfg,还是大写---python的@修饰符解析
- java中对象和json格式相互序列化和反序列化的函数
- Java工程师学习列表
- C++事件的建立和使用SetEvent,ResetEvent