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 ...
随机推荐
- 事件序列化器 Flume 的无数据丢失保证,Channel 和事务
小结: 1.Flume 的持久性保证依赖于使用的持久性Channel 的保证 通过事件序列化器将Flume事件转化为外部存储格式 主要的事件序列化器: 1.文本 2.带有头信息的文本 3.Avro序列 ...
- 谈谈Paxos一致性算法和一致性这个名词
转自:http://www.cnblogs.com/esingchan/p/3917718.html 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的& ...
- Ruby map、each、select、inject、collect 、detect reference
参考 https://ruby-china.org/topics/26718 map:(collect是map的别名函数) 对数组中每个元素进行表达式操作,原始数组不会被改变,返回执行表达式结果的新数 ...
- linux初级学习笔记三:linux操作系统及常用命令,及如何复制和移动文件!(视频序号:02_4)
本节学习的命令:cp,mv,install,du,read 本节学习的技能:文件的移动与复制 cp( copy):复制和移动文件 cp SRC DEST -r:递归复制一个目录及其目录中的所有文件 - ...
- bzoj 1657 Mooo 奶牛的歌声 —— 单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 单调栈水题. 代码如下: #include<iostream> #incl ...
- bzoj 4987 Tree —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4987 其实就是在树上找有 k 个点的连通块(路径上的点都选是最优的),之间的边都走了两遍,只 ...
- asp.net MVC 单选按钮的使用
单选按钮的标准的html 语法 <form><input type="radio" name="sex" value="male&q ...
- ConcurrentHashMap并不是完全的线程安全
ConcurrentHashMap通过分段锁的方式实现了高效率的线程安全,但是它能否在所有高并发场景中都能保证线程安全呢? public class TestClass { private Concu ...
- hibernate的基础学习--一对多关联
基本的用户和部门类,只有uuid和名称,没有其余字段. 配置文件 部门: <?xml version="1.0" encoding="utf-8" ?&g ...
- The IBM Blockchain Platform:Installing the development environment
Follow these instructions to obtain the IBM Blockchain Platform: Develop development tools (primaril ...