blob: 3098372adcc77593a97e3099c3360c9790f66151 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# 477. Total Hamming Distance
"""
the hamming distance between two integers is the number of positions at which
the corresponding bits are different. given an integer array 'nums', return
the sum of hamming distances between all the pairs of the integers in 'nums'.
"""
class Solution(object):
def totalHammingDistance(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
bits = [[0, 0] for _ in range(32)]
for i in nums:
for j in bits:
j[i % 2] += 1
i /= 2
return sum(x * y for x, y in bits)
if __name__ == "__main__":
obj = Solution()
print(obj.totalHammingDistance(nums=[4, 14, 2]))
print(obj.totalHammingDistance(nums=[4, 14, 4]))
|