[LeetCode] 477. Total Hamming Distance(位操作)
Description
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 0 to 10^9
- Length of the array will not exceed 10^4.
思路
题意:给定一个数组,其中两两配对,求出所有配对数的汉明距离。
题解:我们知道,汉明距离是两个数其二进制位上不同的位数,因此,对于32位数,遍历二进制位,统计每个数在第一个bit位有多少个数是1,有多少个数是0,然后统计第二个bit位,以此类推。
- class Solution {
- public:
- //59ms
- int totalHammingDistance(vector<int>& nums) {
- int res = 0,len = nums.size();
- for (int i = 0;i < 32;i++){
- int cnt = 0;
- for (int j = 0;j < len;j++){
- cnt += (nums[j] >> j) & 1;
- }
- res += (len - cnt) * cnt;
- }
- return res;
- }
- };
[LeetCode] 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"
Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...
- 【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 ...
- 477 Total Hamming Distance 汉明距离总和
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...
- 477. Total Hamming Distance
class Solution { public: int totalHammingDistance(vector<int>& nums) { ; ; i < ; i++) { ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- Codeforces 916E(思维+dfs序+线段树+LCA)
题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...
- Spring、Spring MVC、Struts2、、优缺点整理(转)
Spring 及其优点 大部分项目都少不了spring的身影,为什么大家对他如此青睐,而且对他的追捧丝毫没有减退之势呢 Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量 ...
- HTML替换元素,非替换元素和控制元素
替换元素:元素内容由标签的属性来设置,标签其实就是一个占位符.替换元素因为元素内容来自外部资源,所以这些标签大多具有src,指明要引入的资源路径,所以大多仅需要一个标签就可以.例如:<link ...
- CSS中如何设置父元素透明度不影响子元素透明度
原因分析: 使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即便重定义也没有用,不过有个方法可以实现,大家可以看看. 可以使用一张透明的图片做背景可以达成效果 ...
- 行人重识别(ReID) ——数据集描述 CUHK03
数据集简介 CUHK03是第一个足以进行深度学习的大规模行人重识别数据集,该数据集的图像采集于香港中文大学(CUHK)校园.数据以"cuhk-03.mat"的 MAT 文件格式存储 ...
- 解决SVN异常 cleanup failed
winndows上偶尔使用SVN的时候就会整出一些有的没的问题,比如"cleanup failed to process the following paths previous opera ...
- python基础--内置函数filter,reduce
movie_people=["sb+_alex","sb_wupeiqi","han"] # def filter_test(array): ...
- [NOI2007]社交网络(最短路)
[NOI2007]社交网络 Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之 ...
- 三、ARM 寄存器及异常处理
3.1 ARM 内部寄存器 ARM920T 总共有 37 个寄存器,其中 31 通用 32 位寄存器和 6 个状态寄存器,但不能在同一时刻对所有的寄存器可见.处理器状态和运行模式决定了哪些寄存器对程序 ...
- [web 安全] 源码泄露
web 源码泄露 1..hg 源码泄露 http://www.example.com/.hg/ 2..git 源码泄露 http://www.example.com/.git/config 3..ds ...