【leetcode】Reverse Bits
题目描述
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?
解题思路:
就是每次放一个位上去,下面的代码主要是通过对1的移位判断有当前机器上无整型数有多少位
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
decltype(n) result = 0;
int count = 0;
for(decltype(n) i =1; i!=0;i<<=1){
cout<<i<<endl;
result = (result<<1)+(n>>count &1);
count ++;
}
return result;
}
};
PS:另外有一种用merge的方法做的O(n)复杂度的方法


浙公网安备 33010602011771号