您的位置:首页 > 其它

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: