# 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;
}

04-28 152

#### 477. 汉明距离总和

12-05 308

#### LeetCode 477. 汉明距离总和（位运算） 03-09 536

#### 【LeetCode】477. Total Hamming Distance 解题报告（Python & C++）

12-30 811

#### [leetcode, python] Total Hamming Distance 多个数字之间的汉明距离

12-05 126

#### 477.汉明距离总和

05-13 57

#### Leetcode 477：汉明距离总和

08-10 562

#### 汉明距离总和

12-08 7269

#### Hamming Distance汉明距离

01-03 675

#### LeetCode--461. Hamming Distance(汉明距离)Python

09-13 192

#### 461. Hamming Distance(汉明距离)

12-13 230

#### leetcode 477. Total Hamming Distance 任意两数字汉明距离和+32Bit遍历求解

12-07 135

#### 想求汉明距离，怎么提高效率

02-06

12-27 888

#### Leetcode 477. Total Hamming Distance-java

09-11 188

#### Total Hamming Distance 汉明距离总和

09-05 329

#### Leetcode#477. 汉明距离总和

07-10 21万+

#### Java 最常见的 200+ 面试题：面试必备 ©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值