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

解题思路:

一开始也是首先想到了暴力破解法,不过我觉得应该是过不了OJ的

看了GrandYang的解法

对所有数字的每一位统计1的个数和0的个数

这一位的总 汉明距离 为1的个数乘以0的个数

代码:

class Solution {
public:
    int totalHammingDistance(vector<int>& nums) {
        int ret = 0;
        int n = nums.size();
        for(int i = 0; i < 32; i++){
            int zero = 0;
            int one = 0;
            int mode = 1;
            mode <<= i;
            for(int j = 0; j < nums.size(); j++){
                if(nums[j] & mode) one++;
                else zero++;
            }
            ret += zero * one;
        }
        return ret;
    }
};

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9276202.html

时间: 2024-10-10 08:32:25

477. Total Hamming Distance的相关文章

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

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