Leetcode 190.颠倒二进制位 By Python

颠倒给定的 32 位无符号整数的二进制位。

示例:

输入: 43261596
输出: 964176192
解释: 43261596 的二进制表示形式为 00000010100101000001111010011100 ,
     返回 964176192,其二进制表示形式为 00111001011110000010100101000000 。

思路

用python内置的bin()函数转为为二进制数,并用zfill()方法补足到32位,最后将字符串反转,转换为整型即可

代码

class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        return int(bin(n)[2:].zfill(32)[::-1], base=2)

猜你喜欢

转载自www.cnblogs.com/MartinLwx/p/9695441.html