原题链接在这里:https://leetcode.com/problems/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:

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.

题解:

对于int的32位的每一位,计算在这一位是1的array element个数k, 那么在这一位是0的个数就是nums.length-k. 对于这一位就有k*(nums.length-k)种选法可以产生Hamming Distance. 然后将32位Hamming Distance 个数相加就是总数.

Time Complexity: O(1).

Space: O(1).

AC Java:

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

类似Hamming Distance.

LeetCode Total Hamming Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  4. [LeetCode] 477. Total Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  5. [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  8. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  9. LeetCode 461. Hamming Distance (汉明距离)

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. gcd推导

    欧几里得算法有性质: gcd(a, b)=gcd(b, a%b); 那么如何证明呢~ 法1: 我们先假设其成立并且有 gcd(a, b)=gcd(b, a%b)=d; a=k*b+c即a%b=c(我们 ...

  2. hdfs的读写数据流

    hdfs的读:      首先客户端通过调用fileSystem对象中的open()函数读取他需要的的数据,fileSystem是DistributedFileSystem的一个实例, Distrib ...

  3. 分页显示中关于"序号"的问题

    项目开发中要求列表显示要明显看到总条目数,所以就要求序号从1开始. 如下为从1开始的序号展示: <s:iterator value="#request.pageView.records ...

  4. AgileEAS.NET SOA 中间件平台5.2版本下载、配置学习(一):下载平台并基于直连环境运行

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  5. vi使用

    vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后一行首 vi + ...

  6. Delphi中滚动文字的应用

    1.添加一个Timer控件,Interval属性设置为20. 2.添加一个Label控件,Name为labMessage. 3.在Timer的OnTimer事件添加如下代码: procedure TF ...

  7. JS 生成GUID 方法

    var Guid={NewGuid: function () { var guid = (this._G() + this._G() +"-"+ this._G() +" ...

  8. NOI 题库 6264

    6264  走出迷宫 描述 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的 ...

  9. Spring—Quartz定时调度CronTrigger时间配置格式的实例

    格式说明:[秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , - * / 2 分 是 0-59 , - * / 3 小 ...

  10. Python之路第一课Day1--随堂笔记

    课堂大纲: 一.Python介绍 二.发展史 三.Python 2 or 3? 四.安装 五.Hello World程序 六.变量 七.用户输入 八.模块初识 九..pyc是个什么鬼? 十.数据类型初 ...