Skip to content

Latest commit

 

History

History
28 lines (16 loc) · 448 Bytes

476._Number_Complement.md

File metadata and controls

28 lines (16 loc) · 448 Bytes

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 # 这样也可以