zoukankan      html  css  js  c++  java
  • 158-190. 颠倒二进制位

    颠倒给定的 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)
    
  • 相关阅读:
    curl continue
    actor
    nginx
    jmx additional port
    diff
    lsof
    zk reconnect
    Python:Python基础(一)
    Python:初识Python(二)
    Python:初识Python(一)
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14334181.html
Copyright © 2011-2022 走看看