Efficient 1-liner in Python


  • 0
    O
    class Solution(object):
        def findComplement(self, num):
            return -num-1 + 2**int(math.log(num, 2)+1)
    

    math.log(num, 2)+1 is the length of the integer’s binary representation (no leading zero bits)


Log in to reply
 

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