Easiest solution. Python 2 lines


  • 0
    L
    1. Basically get the len of the binary representation of that string.
    2. Make 1's of that and subtract the number from that

    E.g. 5 -> 101. Subtract from 111 and we get 010.
    1 -> 1. Subtact from 1 and we get 0

        def findComplement(self, num):
            """
            :type num: int
            :rtype: int
            """
            allOnes = '1' * (len(bin(num)) -2)
            return int(allOnes, 2) - num
    

Log in to reply
 

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