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 ...
随机推荐
- jvm 命令
jps jps主要用来输出JVM中运行的进程状态信息.语法格式如下: jps [options] [hostid] 如果不指定hostid就默认为当前主机或服务器. -q 不输出类名.Jar名和传入 ...
- HDU 6109 数据分割 【并查集+set】 (2017"百度之星"程序设计大赛 - 初赛(A))
数据分割 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- id、NSObject *、id<NSObject>、instancetype
1. id 与 NSObject * (1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的 ...
- input框只允许输入正整数、正数(包含小数)的解决方法 vue.js实现
我来打自己脸了!!!!...刚刚发现在中文输入法下是无效的,有人能解决这个问题么 如果要求input只能输入数字怎么做? 设置type="number" ? 那我如果想限制长度,此 ...
- EOS智能合约为何选择Web Assembly(wasm)
比特币的程序非常简单,由解锁脚本和锁定脚本构成.以太坊有智能合约,有图灵完备的虚拟机EVM,但是指令也相对简单,且自成一套.这两种程序本质上都是脚本程序,即由程序翻译指令并执行,而不是由本地机器CPU ...
- html5--6-44信纸设计
html5--6-44信纸设计 实例 <!doctype html> <html> <head> <meta charset="utf-8" ...
- 获取指定类型如枚举/属性上自定义attribue值
有时候我们用数字来区分一些类型,如1:中国银行,2:建设银行,3:工商银行,…….这时候我在代码中通常会定义枚举来与定义的一一对应,并在该枚举值上设置特性来表示所代表的含义,这样避免多处写一些数字来标 ...
- 如何生成Android的keystore文件
步骤 1 找到本机电脑上jdk安装的目录,使用cmd命令打开命令窗口,输入cd jdk目录(替换成你的jdk的bin目录),进入到jdk的bin目录,接下来你才可以使用jdk的命令进行操作 步骤 ...
- javascript break 和continue
break语句还可以跳出循环,也就是结束循环语句的执行. continue语句的作用为结束本次循环,接着进行下一次是否执行循环的判断. continue与break的区别是:break是彻底结束循环, ...
- E20180410-hm
preface n. 序言,引语; 开端,前奏; [宗] (弥撒的) 序诵,序祷; vi. 作序; 作为…的序言,作为…的开端; 给…作序; 开始,导致; continue vi. 持 ...