/*
思路:利用位运算,每次拿到最右面的一位值 把他加到(结果左移)里面 最后就得到了 他的 reverse
*/
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int result = 0;
for(int i = 0; i < 32; i++) {
int temp = n & 1;
result = result << 1 | temp;
n = n >> 1;
}
return result;
}
}
Reverse Bits
最新推荐文章于 2021-02-16 08:12:48 发布