Skip to content

476 Number Complement

476. Number Complement

题目: https://leetcode.com/problems/number-complement/

难度: Easy

class Solution(object):
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        i = 1 << (len(bin(num)) -2) # 因为bin函数转化成的格式是‘0bXXXX’,头两个‘0b’要减掉去
        return (i - 1) ^ num
        # return (i - 1) - num # 这样也可以


回到顶部