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到 10^9。
数组的长度不超过 10^4。
详见:https://leetcode.com/problems/total-hamming-distance/description/
C++:
- class Solution {
- public:
- int totalHammingDistance(vector<int>& nums) {
- int res=0,n=nums.size();
- for(int i=0;i<32;++i)
- {
- int cnt=0;
- for(int num:nums)
- {
- if(num&(1<<i))
- {
- ++cnt;
- }
- }
- res+=cnt*(n-cnt);
- }
- return res;
- }
- };
参考:http://www.cnblogs.com/grandyang/p/6208062.html
477 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] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- 477. Total Hamming Distance总的二进制距离
[抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...
- 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 ...
- 461. Hamming Distance + 477. Total Hamming Distance
▶ 与 Hamming 距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...
- LeetCode "477. Total Hamming Distance"
Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...
- 477. Total Hamming Distance
class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- 20170218 OO-ALV标准工具栏按钮
原文地址:OO ALV 工具栏对于的功能码 图标与对应的 功能码 明细 &DETAIL 检查 &CHECK 刷新 &REFRESH 剪切 &LOCAL&CU ...
- lc.exe 已退出 代码为 -1
地址:http://jingyan.baidu.com/article/91f5db1bd0ace31c7f05e321.html
- 怎么显示隐藏Mac上的隐藏文件
打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...
- spring cloud 启动报错-must be declared as an @AliasFor [serviceId], not [name].
项目加入FeignClient后再启动就报错,具体报错信息如下: org.springframework.core.annotation.AnnotationConfigurationExceptio ...
- 牛客练习赛42 E.热爆了
这可能是全场最长的一份代码 问的其实是对于关键点的斯坦纳树大小 考虑补集转化,不合法的点就是它的子树中没有关键点的点和斯坦纳树根的祖先 树根不难求,关键点中dfs序最大最小点的LCA就是了 问题在前者 ...
- HDU 5178 pairs —— 思维 + 二分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others) ...
- html5--6-7 CSS选择器4
html5--6-7 CSS选择器4 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...
- USACO 回文的路径
传送门 这道题和传纸条在某些方面上非常的相似.不过这道题因为我们要求回文的路径,所以我们可以从中间一条大对角线出发去向两边同时进行DP. 这里就有了些小小的问题.在传纸条中,两个路径一定是同时处在同一 ...
- ES6之Object
对象属性模型的相关方法: 对象自身所有属性名称 Object.getOwnPropertyNames(obj) //[] 获取某个属性的attribute对象 Object. ...
- 用 SDL2 加载PNG平铺背景并显示前景
上一篇中加载的是BMP,这次可以引用 SDL2_image.lib,加载更多格式的图像. LoadImage函数做了改动,区别在于不用将surface转换成texture了. 环境:SDL2 + VC ...