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 ...
随机推荐
- Vue 之 npm 及 安装的包
1 npm相关 1.1 npm 是 基于Node.js 的,所以要先安装Node.js 在浏览器地址栏输入https://nodejs.org/en/, 进入Node.js官网后,点击下载左边的稳定 ...
- UI标签库专题三:JEECG智能开发平台 FormValidation(表单提交及验证标签)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28484209 自己定义弹出框提示 ...
- IOS中调用系统拨打电话发送短信
一.调用打电话界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat ...
- ubuntu主板信息
root:~# sudo dmidecode |grep -A16 "System Information$"sudo: 无法解析主机:phone-TPOWER-X79System ...
- /dev下添加设备节点的方法步骤(通过device_create)
将自己开发的内核代码加入到Linux内核中,需要3个步骤: 1.确定把自己开发代码放入到内核合适的位置 将demo_chardev.c文件拷贝到.../drivers/char/目录下. demo_c ...
- js中一些常见写法的含义
1. 常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号 ...
- YTU 2429: C语言习题 学生成绩输入和输出
2429: C语言习题 学生成绩输入和输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 1897 解决: 812 题目描述 编写一个函数print,打印一个学生的成绩数组,该数组中有 ...
- let 命令 与 var的区别
ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. <script> { let a = 10; var b = 1; } ...
- poj 1860 Currency Exchange 解题报告
题目链接:http://poj.org/problem?id=1860 题目意思:给出 N 种 currency, M种兑换方式,Nick 拥有的的currency 编号S 以及他的具体的curren ...
- lock的两种方式
假设现在我们有100个数据项可以读写.有若干个线程,任何一个线程可能对任何一个数据项尽心读写. 但是,如果不同的线程在对同一个数据项进行读写,就可能发生错误.需要使用lock进行控制. 比如线程x要对 ...