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 - k).

class Solution {
    public int totalHammingDistance(int[] nums) {
        if (nums.length < 2) {
            return 0;
        }

        int result = 0;;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                result += getDistance(nums[i], nums[j]);
            }
        }
        return result;
    }

    private int getDistance(int a, int b) {
        int c = a ^ b;
        int result = 0;
        while (c != 0) {
            result++;
            c &= c - 1;
        }
        return result;
    }
}
class Solution {
    public int totalHammingDistance(int[] nums) {
        if (nums.length < 2) {
            return 0;
        }

        int result = 0;;
        for (int i = 0; i < 32; i++) {
            int ones = 0;
            for (int j = 0; j < nums.length; j++) {
                ones += (nums[j] >> i) & 1;
            }
            result += ones * (nums.length - ones);
        }
        return result;
    }
}
时间: 2024-10-07 02:53:08

LeetCode 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

问题描述: 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

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

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

LeetCode之461. Hamming Distance

------------------------------------------------------------------ AC代码: public class Solution { public int hammingDistance(int x, int y) { return Integer.toString(x^y,2).replaceAll("0","").length(); } } 题目来源: https://leetcode.com/prob

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

LeetCode题解之Hamming Distance

1.题目描述 2.问题分析 使用C++ 标准库中的 bitset 类,将整数转换为二进制形式,然后再将其转换为字符串,最后比较字符串. 3.代码 1 int hammingDistance(int x, int y) { 2 3 bitset<32> a(x); 4 bitset<32> b(y); 5 6 string a_s = a.to_string(); 7 string b_s = b.to_string(); 8 9 string::iterator it1 = a_s