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
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:
- Elements of the given array are in the range of 0 to 10^9
- Length of the array will not exceed 10^4.
分析
题目的意思是:求出所有数中两两之间的汉明距离。
例子,4,14,2和1:
4: 0 1 0 0
14: 1 1 1 0
2: 0 0 1 0
1: 0 0 0 1
- 先看最后一列,有三个0和一个1,那么它们之间相互的汉明距离就是3,即1和其他三个0分别的距离累加,然后在看第三列,累加汉明距离为4,因为每个1都会跟两个0产生两个汉明距离,同理第二列也是4,第一列是3。我们仔细观察累计汉明距离和0跟1的个数,可以发现其实就是0的个数乘以1的个数,发现了这个重要的规律,那么整道题就迎刃而解了,只要统计出每一位的1的个数即可。
- 根据规律,只需要统计每一列中1的个数,0的个数,然后相乘,然后加入结果res中,就是答案了。
代码
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int res=0;
int n=nums.size();
for(int i=0;i<32;i++){
int cnt=0;
for(int num:nums){
if(num&(1<<i)){
cnt++;
}
}
res+=cnt*(n-cnt);
}
return res;
}
};
参考文献
[LeetCode] Total Hamming Distance 全部汉明距离