My Python Solution


  • 1
    D

    For some reason unknown, if I put this solution in one-line, the computation time will double

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

  • 0
    W

    Similiarly:

    class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        s = bin(n)[2:]
        
        s = '0b' + s[::-1].ljust(32, '0')
        
        return int(s, 2)
    

    Thank you for your good idea.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.