Problem 190: Reverse Bits
思路
这道题的思路就是首先保留原先的 bit,同时 reverse bit 的顺序
保留原先 bit :
n & 1
,这样 0 还是 0 ,1 还是 1然后用 shift 的形式连接 bit,原来是从右向左,现在一连接就相当于 “reverse” 了 bits
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int rst = 0;
for (int i = 0; i < 32; i++) {
rst += (n & 1);
n >>>= 1;
if (i < 31) rst <<= 1;
}
return rst;
}
}
Follow Up
If this function is called many times, how would you optimize it?
PreviousProblem 421: Maximum XOR of Two Numbers in an ArrayNextProblem 318: Maximum Product of Word Lengths
Last updated
Was this helpful?