Python One Liner


  • 0
    K

    Subtract the binary value of a number from a string of 1s of the same length as that of binary. Although it's a decimal subtraction, it'll work because of 1s and 0s. Treating that number as a binary, convert into decimal. It's done by string manipulation here in this simple code.

    def findComplement(self, num):
            """
            :type num: int
            :rtype: int
            """
            return int(str(eval('1'*len(bin(num)[2:]) + '-' + bin(num)[2:])), 2) 
    

Log in to reply
 

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