461. 汉明距离
解法一:
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
count = 0
while x != y:
count += x&1 ^ y&1
x >>= 1
y >>= 1
return count解法二:
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
#转二进制字符串,截取数字部分,统计1
return bin(x ^ y)[2:].count('1')最后更新于