Number Complement

来源:互联网 发布:斯凯网络 编辑:程序博客网 时间:2024/04/26 20:59
class Solution(object):    def findComplement(self, num):        """        :type num: int        :rtype: int        """        flag = 0        for i in range(31,-1,-1):            if (num & (1 << i)) or flag:                flag |= 1 << i        return (flag)^num

class Solution2(object):    def findComplement(self, num):        i = 1        while i <= num:            i <<= 1        return (i - 1) ^ num

原创粉丝点击