Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 407 Bytes

476.md

File metadata and controls

19 lines (17 loc) · 407 Bytes

476. Number Complement

Solution 1 (time O(log(n)), space O(1))

class Solution(object):
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        ans = 0
        cur_base = 1
        while num > 0:
            ans += (1 - (num % 2)) * cur_base
            num = num // 2
            cur_base *= 2
        return ans