颠倒给定的 32 位无符号整数的二进制位(硬性拼字符串就是我的)
class Solution:
# @param n, an integer
# @return an integer
def reverseBits1(self, n):
binary_ = lambda n: "" if n == 0 else binary_(n >> 1) + str(n & 1)
binary_str = binary_(n).rjust(32, "0")[::-1]
ret_num = int(binary_str, 2)
return ret_num
def reverseBits2(self, n: int) -> int:
return int(f'{n:032b}'[::-1], 2)
def reverseBits(self, n):
ret, power = 0, 31
while n:
ret += (n & 1) << power
n = n >> 1
power -= 1
return ret
if __name__ == '__main__':
s1 = Solution()
n = 43261596
root = s1.reverseBits(n)
print(root)