1009. Complement of Base 10 Integer

题目

在这里插入图片描述

我的代码

只要找到补的2*n-1 得到剩下的补。

class Solution(object):
    def bitwiseComplement(self, N):
        """
        :type N: int
        :rtype: int
        """
        i=2
        while N>=i:
            i*=2
        return i-N-1

优秀代码

class Solution(object):
    def bitwiseComplement(self, N):
        """
        :type N: int
        :rtype: int
        """
        """
        s = bin(N)
        res = ''
        for i in s[2:]:
            res += '0' if i == '1' else '1'
        return 0 if all(r == '0' for r in res) else int(res[res.index('1'):],2)
        """
        return 2**(len(bin(N))-2) - N -1
            

猜你喜欢

转载自blog.csdn.net/xiabenshu/article/details/88985519