美文网首页Leetcode刷题笔记
第四十四天 Number Complement

第四十四天 Number Complement

作者: 业余马拉松选手 | 来源:发表于2018-10-22 23:11 被阅读11次

最近因为孩子生病的事情,状态全无,心思不在
刷道水题,嗯

https://leetcode-cn.com/problems/number-complement/

对于位运算,一直掌握的不好,这道题其实就是按位与,配着左移运算符,但是否真正理解了呢?嗯,还需要多看和多写

class Solution:
    def findComplement(self, num):
        """
        :type num: int
        :rtype: int
        """
        i = 1
        while num >= i:
            num ^= i
            i <<= 1
        return num

相关文章

网友评论

    本文标题:第四十四天 Number Complement

    本文链接:https://www.haomeiwen.com/subject/nxgnzftx.html