LeetCode 190:Reverse Bits
2015-12-17 21:24
288 查看
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?
反转一个32位的无符号数。
例如,给定输入为43261596(二进制表示为00000010100101000001111010011100),应该返回964176192(二进制表示为00111001011110000010100101000000)
思考:能不能降低函数的调用次数?
然而我用的最蠢的办法 —— 一个个读←_←话说今天状态不太好,而且明天还有操作系统实验,所以大概会少做几道,之后会补上的。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t temp=0;
int i=0;
for(;i<32;i++)
{
temp*=2;
temp+=((n>>i)&0x01);
}
return temp;
}
};
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?
反转一个32位的无符号数。
例如,给定输入为43261596(二进制表示为00000010100101000001111010011100),应该返回964176192(二进制表示为00111001011110000010100101000000)
思考:能不能降低函数的调用次数?
然而我用的最蠢的办法 —— 一个个读←_←话说今天状态不太好,而且明天还有操作系统实验,所以大概会少做几道,之后会补上的。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t temp=0;
int i=0;
for(;i<32;i++)
{
temp*=2;
temp+=((n>>i)&0x01);
}
return temp;
}
};
相关文章推荐
- 多语言对比
- Openwrt UCI 快捷切换SS 配置
- JAVA布局基础正确源代码
- 第十四周(项目一)--二叉排序树相关算法
- OBJECT和EMBED标签
- Openwrt UCI 快捷切换SS 配置
- 黑马程序员_java网络编程
- poj1703
- [斯坦福创业课]Jeff谈LinkedIn的同理心管理
- Centos上Apache重启,mysql重启, nginx 重启方法
- 谷歌电子市场学习笔记第二天之下
- C++ 图的深度搜索广度搜索和最小生成树代码实现
- 【气象助手项目开发】-第三篇:聚合平台WebService技术
- Java 控制台模拟 福彩3D 系统
- How to use kingshard building a MySQL cluster
- 总结
- 网络流男女配对问题java实现
- Voice LAB-1 CUBE Cisco Unified Border Element
- 海量数据处理学习
- UI基础-MVC、通知