Skip to content

Latest commit

 

History

History
21 lines (17 loc) · 501 Bytes

190.-dian-dao-er-jin-zhi-wei.md

File metadata and controls

21 lines (17 loc) · 501 Bytes

190. 颠倒二进制位

https://leetcode-cn.com/problems/reverse-bits/

解法一:

class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        #int转成二进制字符串
        #除去‘0b’开头后反转
        str1 = bin(n)[2:][::-1]
        #补0
        if len(str1) < 32:
            str1 += '0'*(32-len(str1))
        #转成二进制int
        return(int(str1, 2))