Easy-to-understand python solution


  • 0
    P
    class Solution(object):
        def findComplement(self, num):
            """
            :type num: int
            :rtype: int
            """
            complement = 0
            digit = 0
            while num > 0:
                if num % 2 == 0:
                    complement += 2 ** digit
                digit += 1
                num >>= 1
            return complement
    

Log in to reply
 

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