顿搜
飞过闲红千叶,夕岸在哪
类目归类
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?
Input: 43261596
Ouput: 964176192
public int reverseBits(int n) {
for (int i = 0; i < 16; i++) {
int lowBit = (n >> i) & 1;
int highBit = (n >> (31 - i)) & 1;
if ((lowBit ^ highBit) == 1) {
n ^= (1 << i) | (1 << 31 - i);
}
}
return n;
}public class LeetCode0190 {
public int reverseBits(int n) {
for (int i = 0; i < 16; i++) {
int lowBit = (n >> i) & 1;
int highBit = (n >> (31 - i)) & 1;
if ((lowBit ^ highBit) == 1) {
n ^= (1 << i) | (1 << 31 - i);
}
}
return n;
}
public static void main(String[] args) {
LeetCode0190 leetcode = new LeetCode0190();
System.out.println(leetcode.reverseBits(43261596));
}
}