Python Simplest (maybe) Solution, No loops


  • 0
    Y

    Algo: the num^ans must equal to '1'*len(num)

    class Solution(object):
        def findComplement(self, num):
            
            x = len(bin(num)[2:])
            m = int('1'*x,2)
            return m^num
    
    

Log in to reply
 

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