Simple Python Solution without loops. Making use of MSB of a binary number.

  • 0

    We can make use of "bit_length()" function of python to eliminate some looping.

    We can do something like this

    class Solution(object):
        def findComplement(self, num):
            :type num: int
            :rtype: int
            msb = num.bit_length()
            return (2**msb-1) ^ num

Log in to reply

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