# LeetCode 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.

## 解题代码

    public int totalHammingDistance(int[] nums) {
int sum = 0;
for (int i = 0; i < 32; i++) {
int ones = 0;
for (int j = 0; j < nums.length; j++)
ones += (nums[j] >> i) & 0x1;
sum += ones * (nums.length - ones);
}
return sum;
} 博客
477. 汉明距离总和
04-21 1496 博客
Leetcode#477. 汉明距离总和
09-05 382 博客
Leetcode ---- 477. 汉明距离总和
12-07 187 博客

08-10 719 博客
[leetcode, python] Total Hamming Distance 多个数字之间的汉明距离
12-30 919 博客
Leetcode Total Hamming Distance 477
12-18 770 博客
LeetCode--461. Hamming Distance(汉明距离)Python
01-03 1040 博客
LeetCode 477. Total Hamming Distance-python
12-27 910 博客
477.汉明距离总和
12-05 178 博客
LeetCode 477. 汉明距离总和（位运算）
12-05 668 博客
LeetCode-Python-477. 汉明距离总和 (位运算）
11-08 288 博客
Total Hamming Distance 汉明距离总和
09-11 349 博客
Leetcode 477：汉明距离总和
05-13 104 博客
leetcode 477
09-27 213

### “相关推荐”对你有帮助么？

•  非常没帮助
•  没帮助
•  一般
•  有帮助
•  非常有帮助 _海阔天空

¥2 ¥4 ¥6 ¥10 ¥20  余额支付 (余额：-- )  扫码支付 获取中  扫码支付 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值