Problem
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, return the Hamming distance between them.
Algorithm
Use xor to compute the differing bits, then count them.
Code
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
xy = x^y
ans = 0
while xy:
ans += xy % 2
xy //= 2
return ans

412

被折叠的 条评论
为什么被折叠?



