LeetCode 477. Total Hamming Distance

原创 2016年12月24日 14:37:11

欢迎移步到我的个人博客

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

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

题意解析

计算一个数组中,任意两个数汉明距离的总和。

解法分析

对于一个数组中,全部数字二进制的任意一位,汉明距离的总和为1的个数乘以0的个数。而数组中的数皆为int类型,所以一共需要进行32次循环。

解题代码

    public int totalHammingDistance(int[] nums) {
        int sum = 0;
        for (int i = 0; i < 32; i++) {
            int ones = 0;
            for (int j = 0; j < nums.length; j++)
                ones += (nums[j] >> i) & 0x1;
            sum += ones * (nums.length - ones);
        }
        return sum;
    }

[leetcode, python] Total Hamming Distance 多个数字之间的汉明距离

The Hamming distance between two integers is the number of positions at which the corresponding bits...
  • ysk0825
  • ysk0825
  • 2016-12-30 01:16:26
  • 423

[leetcode]461. Hamming Distance --python

Description The Hamming distance between two integers is the number of positions at which the corr...
  • li2011014203
  • li2011014203
  • 2017-02-28 19:29:37
  • 373

LeetCode 477. Total Hamming Distance-python

题目大意: 两个整数的汉明距离是指其二进制不相等的位的个数。 计算给定的整数数组两两之间的汉明距离之和。 注意: 元素大小在[0, 10^9]之间。数组长度不超过10^4。 ...
  • xiaopihaierletian
  • xiaopihaierletian
  • 2016-12-27 21:18:51
  • 711

[Leetcode,python] Hamming Distance 汉明距离

The Hamming distance between two integers is the number of positions at which the corresponding bits...
  • ysk0825
  • ysk0825
  • 2016-12-20 00:12:40
  • 522

477. Total Hamming Distance

477. Total Hamming Distance The Hamming distance between two integers is the number of position...
  • u010213787
  • u010213787
  • 2016-12-27 19:07:30
  • 212

Leetcode 477. Total Hamming Distance-java

1 解题思想 题目的意思就是给了一个数组,现在求总的海明距离,其中:  1、海明距离:任意两个数在二级制的表示下(int = 32bit),每个bit对应的值是1或0,那么这两个数在这32个位置下,...
  • xiaopihaierletian
  • xiaopihaierletian
  • 2016-12-27 21:07:29
  • 541

leetcode 477 Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits...
  • feifeiiong
  • feifeiiong
  • 2017-03-26 21:13:45
  • 140

Leetcode-477. Total Hamming Distance

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2017-01-01 18:34:11
  • 389

477. Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bi...
  • liuchenjane
  • liuchenjane
  • 2016-12-18 12:44:35
  • 1335

Leetcode Total Hamming Distance 477

The Hamming distance between two integers is the number of positions at which the corresponding bits...
  • huaxixidongbeishida
  • huaxixidongbeishida
  • 2016-12-18 16:54:22
  • 633
收藏助手
不良信息举报
您举报文章:LeetCode 477. Total Hamming Distance
举报原因:
原因补充:

(最多只允许输入30个字)