a slightly different solution


  • 0
    Z
    class Solution(object):
        def findComplement(self, num):
            """
            :type num: int
            :rtype: int
            """
            mask = 1
            tmp = num >> 1
            while tmp:
                mask = 1 | (mask << 1)
                tmp >>= 1
            return num ^ mask
    

Log in to reply
 

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