[LeetCode] 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:
- Elements of the given array are in the range of
0to10^9 - Length of the array will not exceed
10^4.
这道题是之前那道 Hamming Distance 的拓展,由于有之前那道题的经验,我们知道需要用异或来求每个位上的情况,那么需要来找出某种规律来,比如看下面这个例子,4,14,2 和1:
4: 0 1 0 0
14: 1 1 1 0
2: 0 0 1 0
1: 0 0 0 1
先看最后一列,有三个0和一个1,那么它们之间相互的汉明距离就是3,即1和其他三个0分别的距离累加,然后在看第三列,累加汉明距离为4,因为每个1都会跟两个0产生两个汉明距离,同理第二列也是4,第一列是3。仔细观察累计汉明距离和0跟1的个数,可以发现其实就是0的个数乘以1的个数,发现了这个重要的规律,那么整道题就迎刃而解了,只要统计出每一位的1的个数即可,参见代码如下:
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int res = , n = nums.size();
for (int i = ; i < ; ++i) {
int cnt = ;
for (int num : nums) {
if (num & ( << i)) ++cnt;
}
res += cnt * (n - cnt);
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/477
类似题目:
参考资料:
https://leetcode.com/problems/total-hamming-distance/
https://leetcode.com/problems/total-hamming-distance/discuss/96226/Java-O(n)-time-O(1)-Space
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Total Hamming Distance 全部汉明距离的更多相关文章
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 477. Total Hamming Distance总的二进制距离
[抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...
随机推荐
- Linux下的磁盘分割和文件系统
一.各硬件装置在Linux下的文件名 1.IDE硬盘机 在Linux内的文件名: /dev/hd[a-d] (a-d 刚好是四个这个是有原因的具体如下) 解释:以 IDE 接口来说,由于一个 IDE ...
- ASP.NET Core 介绍
原文:Introduction to ASP.NET Core 作者:Daniel Roth.Rick Anderson.Shaun Luttin 翻译:江振宇(Kerry Jiang) 校对:许登洋 ...
- 游戏启示录 关于Update的相关问题
游戏启示录 关于Update的相关问题 这里说的Update是指的游戏的主循环.一般情况下.为了程序的方便控制我们一般只会有一个主循环.所有的游戏逻辑都会在这一个循环中完成.(额,其实这么做有点浪费. ...
- Linq在Array,List,Dictionary中的应用
Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...
- 应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案
SSIO的更新 在SSIO上增加了UDP通讯方式,可以到Github上下载源代码.在原来的项目中,远端的设备与中心站的数据交互并没有使用过UDP方式.这种短连接的通讯链路,不容易维护,主要体现在:(1 ...
- axis2+struts拦截地址冲突问题
axis2和struts在整合过程中,struts会把axis的地址也拦截了,默认当成一个action处理, 会因为找不到action而报错: <!-- struts配置 --> < ...
- (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...
- 怎样才能自学好Java?
经常有初学Java的同学问:怎么样才能学好Java?自学Java难吗? 我认为自学Java并不难.相对于其他语言来说,因为Java95年才出来,所以相对比较新,旧的语言中一些不合适的东西在Java里面 ...
- SQL Server导入数据时“启用标示插入”详解
在SQL Server中导入数据时,会有一个"启用标示插入"的选项,突然间懵逼了,这到底啥意思?我选与不选这个选项,结果好像没区别!不科学啊这,"存在即合理", ...
- win7 由ie8升级ie11时安装不成功的一个原因
纯净win7系统更新好补丁好 升级ie一直提示不成功,之前重来没有遇到过.官方提示的解决办法: https://support.microsoft.com/zh-cn/kb/2872074 根据内容初 ...