leetcode-python-汉明距离

时间:2021-06-11 19:07:12   收藏:0   阅读:0

1)取异或,然后计算1的个数

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        t = x^y
        count = 0
        while t:
            count += 1
            t = t& t-1
        return count

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!