190. Reverse the conversion between binary digits (leetcode) python 2, 8, 10, 16 digits

 

 Hex conversion function: 

  1. bin (), oct (), hex () are used to convert the decimal number Interger into binary string ('0b1000'), octal string ('0o10'), hexadecimal string ('0x8') .

  2. int (n, 2), int (n, 8), int (n, 16) can convert 2, 8, 16 string into decimal numbers

  3. s.zfill () fills 0 to the specified number of digits before the string s 

Ideas:

  1. Convert the input number to binary and reverse s = '0b' + bin (s) [2:]. Zfill (32) [::-1]

  2. Convert s to decimal s = int (s, 2)

 

Code:

class Solution:
    def reverseBits(self, n: int) -> int:

        reversedNum = '0b'+bin(n)[2:].zfill(32)[::-1]
        return int(reversedNum,base=2)

 

Guess you like

Origin www.cnblogs.com/ChevisZhang/p/12714508.html