477. Total Hamming Distance 总的汉明距离

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

Input: 4, 14, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

Note:

  1. Elements of the given array are in the range of 0 to 10^9
  2. Length of the array will not exceed 10^4.
  1. class Solution1(object):
  2. def totalHammingDistance(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. res = 0
  8. bins = []
  9. for num in nums:
  10. bins.append(‘{0:b}‘.format(num).zfill(32))
  11. for i in range(32):
  12. bitCount = 0
  13. # zeroCount
  14. for num in bins:
  15. if num[i] is ‘1‘:
  16. bitCount += 1
  17. # zeroCount = (len(nums) - bitCount)
  18. res += bitCount * (len(nums) - bitCount)
  19. return res
  20. class Solution2(object):
  21. def totalHammingDistance(self, nums):
  22. res = 0
  23. for i in range(32):
  24. bitCount = 0
  25. for num in nums:
  26. bitCount += num >> i & 1
  27. res += bitCount * (len(nums) - bitCount)
  28. return res
  29. nums = [4, 14, 2]
  30. s = Solution1()
  31. res = s.totalHammingDistance(nums)
  32. print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8319176.html

时间: 2024-10-10 08:32:26

477. Total Hamming Distance 总的汉明距离的相关文章

[LeetCode] 477. Total Hamming Distance(位操作)

传送门 Description The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Ou

477. Total Hamming Distance

问题描述: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Ex

477. Total Hamming Distance - Medium

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

477 Total Hamming Distance 汉明距离总和

两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系)所以答案为:HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.注意:    数组中元素的范围为从 0到 1

LeetCode 477: Total Hamming Distance

Note: 1. Very smart way of calculating how many difference from bits: https://en.wikipedia.org/wiki/Hamming_distance#Algorithm_example 2. Another way is counting how many 1s and 0s per bits. Then the contribution of that bit will bt C(1, k) * C(1, n

[Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

LeetCode 461. Hamming Distance (汉明距离)

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 231. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

Total Hamming Distance

1 class Solution(object): 2 def totalHammingDistance(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 bin_num = ["{0:b}".format(num) for num in nums] 8 zip_list = [] 9 for i in range(len(bin_num)): 1

相似算法 ,Java实例9 - 汉明距离 Hamming Distance

Java实例9 - 汉明距离 Hamming Distance http://blog.csdn.net/kindterry/article/details/6581344 /**在信息理论中,两个等长字符串之间的汉明距离 * 是两个字符串对应位置上不同字符的个数, * 换句话说,汉明距离就是将一个字符串替换成另外一个字符串所需要替换的字符长度. *例如,1011101和1001001之间的汉明距离是2, *toned和roses之间的汉明距离是3. *汉明重量是字符串相对于同样长度的零字符串的