# Easy
Last updated 4 years ago
Was this helpful?
输入是数字,没有bit这种类型,所以只能左移和右移来操作
让最低位和最高位互换,就是先获取低位然后通过左移至高位,需要遍历所有的位
class Solution: def reverseBits(self, n: int) -> int: res = 0 power = 31 while n: res += (n & 1) << power n = n >> 1 power -= 1 return res
Time = O(1)O(1)O(1) , space = O(1)O(1)O(1) .