An easy to understand Python solution


  • 0
    C
     def findComplement(self, num):
         # convert integer input to binary
         binary = lambda n: '' if n==0 else binary(n/2) + str(n%2)
         # convert binary to binary complement
         complement = ''.join(['1' if letter =='0' else '0' for letter in binary(num)])
         # convert binary complement to integer
         return int(complement,2)           
    

Log in to reply
 

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