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

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 to 10^9
  2. Length of the array will not exceed 10^4.

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.

time = O(n), space = O(1)

class Solution {
    public int totalHammingDistance(int[] nums) {
        int res = 0, n = nums.length;
        for(int i = 0; i < 32; i++) {
            int oneCount = 0;
            for(int j = 0; j < nums.length; j++) {
                oneCount += (nums[j] >> i) & 1;
            }
            int zeroCount = n - oneCount;
            res += zeroCount * oneCount;
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/11403392.html

时间: 2024-11-06 03:49:51

477. Total Hamming Distance - Medium的相关文章

[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 Explanat

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

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

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

[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

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

HDU 4712 Hamming Distance (随机函数)

Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1806    Accepted Submission(s): 714 Problem Description (From wikipedia) For binary strings a and b the Hamming distance is equal

hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 630 Problem Description (From wikipedia) For bina