1. 461. 汉明距离
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x^y).count('1')
2. 477.汉明距离总和
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
for i in range(30):
c = sum(((val >> i) & 1) for val in nums)
ans += c * (n - c)
return ans