https://leetcode.com/problems/total-hamming-distance/
https://leetcode.com/problems/total-hamming-distance/discuss/96226/java-on-time-o1-space
这题brute force的做法是两重for循环,用上一题的方法,两两比较,O(n ^ 2)的时间复杂度,超时了。
O(n)的复杂度的做法是,外层的for循环遍历int上的每一位,内层的for循环遍历数组,统计每一位出现了1的count,那该位的hamming distance就是count * (array.length - count)。
May use the bitwise operation to reduce the time complexity. All the numbers can be viewed as 32 bits with 0s and 1s. So for each bit, we just need to count the total number of 1s, then the total number of 0s will be (total numbers - the numbers of 1s). And the contribution to the Hamming distance will be count of 1s times the count of 0s. Similarly argument can be applied to all the rest bits.
X.
https://www.jianshu.com/p/8f127578fe73
https://zhuhan0.blogspot.com/2017/08/leetcode-477-total-hamming-distance.html
X. Brute Force
https://github.com/veronikabenkeser/LeetCode/blob/master/477.%20Total%20Hamming%20Distance
http://blog.csdn.net/zhouziyu2011/article/details/53726081
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
to10^9
- Length of the array will not exceed
10^4
.
https://leetcode.com/problems/total-hamming-distance/discuss/96226/java-on-time-o1-space
For each bit position 1-32 in a 32-bit integer, we count the number of integers in the array which have that bit set. Then, if there are n integers in the array and k of them have a particular bit set and (n-k) do not, then that bit contributes k*(n-k) hamming distance to the total.
public int totalHammingDistance(int[] nums) {
int total = 0, n = nums.length;
for (int j=0;j<32;j++) {
int bitCount = 0;
for (int i=0;i<n;i++)
bitCount += (nums[i] >> j) & 1;
total += bitCount*(n - bitCount);
}
return total;
}
https://zyfu0408.gitbooks.io/interview-preparation/content/bit-manipulation/bit.html这题brute force的做法是两重for循环,用上一题的方法,两两比较,O(n ^ 2)的时间复杂度,超时了。
O(n)的复杂度的做法是,外层的for循环遍历int上的每一位,内层的for循环遍历数组,统计每一位出现了1的count,那该位的hamming distance就是count * (array.length - count)。
public int totalHammingDistance(int[] nums) {
int total = 0;
for (int i = 0; i < 32; i++) {
int count = 0;
for (int num: nums) {
if ((num >> i & 1) == 1) {
count++;
}
}
total += count * (nums.length - count);
}
return total;
}
public int hammingDistance(int x, int y) {
int count = 0;
for (int i = 0; i < 32; i++) {
int n1 = x >> i & 1;
int n2 = y >> i & 1;
if (n1 != n2) {
count++;
}
}
return count;
}
http://startleetcode.blogspot.com/2016/12/477-total-hamming-distance.html- int totalHammingDistance(vector<int>& nums) {
- int res = 0;
- for(int i=0; i<32; ++i){
- int one = 0;
- for(int j=0; j<nums.size(); ++j){
- if((1<<i) & nums[j]) ++one;//one is the numbers of 1s;
- }
- res += one*(nums.size()-one);// nums of 1s x nums of 0s
- }
- return res;
- }
按位统计各整数的二进制0与1的个数之和,分别记为zero[i], 和one[i]
def totalHammingDistance(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = 0
for x in range(32):
mask = 1 << x
zero = one = 0
for num in nums:
if num & mask:
one += 1
else:
zero += 1
ans += zero * one
return ansX.
https://www.jianshu.com/p/8f127578fe73
那么应该怎么做这道题目呢?题目中的 Explanation 其实是一种误导,我们真的有必要两两组合分别求 Hamming Distance 吗?其实是没有必要的,一次循环能否取得所有的 Hamming Distance?
通过分析得出,Hamming Distance 其实就是每一位的异同。那么这道题目就可以转化为:求x个数每两个数的组合中,每一位的异同数量。想到这里,就会发现其实x个数中每个数的数值并不重要,重要的是每一位有多少个0和多少个1。
通过分析得出,Hamming Distance 其实就是每一位的异同。那么这道题目就可以转化为:求x个数每两个数的组合中,每一位的异同数量。想到这里,就会发现其实x个数中每个数的数值并不重要,重要的是每一位有多少个0和多少个1。
A more efficient way to solve this is to iterate through the bits. Since max(nums) < 10^9, I know that the numbers won't take more than 32 bits. Say for one of these bits, there are k numbers with that bit on, so there are n - k numbers with that bit off. Then that particular bit will add k(n - k) to the total hamming distance.
public int totalHammingDistance(int[] nums) { int[] bitCounts = new int[32]; for (int num : nums) { int i = 0; while (num > 0) { bitCounts[i] += num & 1; num >>= 1; i++; } } int length = nums.length; int total = 0; for (int count : bitCounts) { total += count * (length - count); } return total; }
https://github.com/veronikabenkeser/LeetCode/blob/master/477.%20Total%20Hamming%20Distance
http://blog.csdn.net/zhouziyu2011/article/details/53726081
(1)遍历数组nums,对每一个nums[i],求其余后面的每一个nums[j]的Hamming Distance。
(2)求x与y的Hamming Distance的方法:
---1)先求x^y的结果res。
---2)再依次求32位res的每一位与1进行与操作的结果,若不为0,则Hamming Distance加一。
- public int totalHammingDistance(int[] nums) {
- int len = nums.length;
- if (len == 0)
- return 0;
- int result = 0;
- for (int i = 0; i < len - 1; i++) {
- for (int j = i + 1; j < len; j++)
- result += hammingDistance(nums[i], nums[j]);
- }
- return result;
- }
- public int hammingDistance(int x, int y) {
- int res = x ^ y;
- int count = 0;
- for (int i = 0; i < 32; i++) {
- if ((res & 1) != 0)
- count++;
- res >>= 1;
- }
- return count;
- }