题目

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
            

相关文章:

  • 2021-08-20
  • 2022-01-19
  • 2021-08-21
  • 2022-12-23
猜你喜欢
  • 2021-07-01
  • 2022-12-23
  • 2022-03-04
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案