[LeetCode]Number Complement - Python

来源:互联网 发布:淘宝能发货到台湾吗 编辑:程序博客网 时间:2024/06/06 01:17

给定一个正整数,输出它的补数,补数就是翻转原数二进制形式的每一位,如5(101)的补数为2(010)。

class Solution(object):def findComplement(self, num):    """    :type num: int    :rtype: int    """    i = 1    while i <= num:        num ^= i        i <<= 1    return num
0 0
原创粉丝点击