[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 ...
随机推荐
- Eclipse的Working Set管理项目
想必大家的Eclipse里也会有这么多得工程...... 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使用的, 必须大规模的滚动滚动条......有点不和谐了. ...
- P2933 [USACO09JAN]气象测量The Baric Bovine
传送门 挺显然的 $dp$ ,然鹅一开始想的是 $dfs$ 乱剪剪枝搞了 $70$ 分... 设 $f[i][j]$ 表示切了 $i$ 次,当前切的位置为 $j$ 的最小误差 那么转移显然枚举上一个切 ...
- github提交用户权限被拒
场景介绍: 之前登陆了朋友的github账号,保存了朋友的GitHub信息在本地.今天想重新提交一个项目到自己的GitHub账号时,一直用朋友的账号提交且提示权限被拒. 解决: 方法一,GitHub配 ...
- basename函数不能获取url路径中文文件名的问题
basename basename() 函数返回路径中的文件名部分. 语法 basename(path,suffix) 参数 描述 path 必需.规定要检查的路径. suffix 可选.规定文件扩展 ...
- Python的五大数据类型的作用、定义方式、使用方法
一.简述Python的五大数据类型的作用.定义方式.使用方法: 1. 数字类型int: 1.整形 作用:可以表示人的年龄,身份证号码,身高和体重等 定义方式: weight = 130 print( ...
- Vue实现点击li变色
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Solr的学习使用之(十)数据库(Oracle、SqlServer)原有的全文索引功能和Solr对比?
本人有个问题一直不解,既然solr的全文索引功能这么强大,而且效果也不错,那为什么那些数据库厂商比如Oracle.SqlServer,不把solr的功能集成进去呢,或者说把全文索引的功能做好点,做到和 ...
- 认知redis
一.redis是什么? 1.基于key-value的内存No sql 数据库(非关系型数据库) 2.读写性能非常好 二.redisd的数据类型有哪些?特点分别是什么? 1)string 一个键对一个值 ...
- 09.Linux系统由于不正常关机导致的分区问题
问题:Error:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY Give root password for maintenance ------------ ...
- Linux 内核层和 用户层 配置 GPIO 引脚
Linux BSP 开发的基础就是和GPIO打交道, 下面总结下这几天对某家开发板的GPIO控制的知识. 公司的开发板用的是 DTB 模式 ,首先,进入 dts,dtsi文件查看关于GPIO 的模块 ...