【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.
题目大意
计算数组中所有数字之间的汉明距离。
解题方法
位运算
汉明距离可以通过异或操作去算。
数组长度会达到10^4, 暴力解法不可取。
思路:计算数组中每个数的二进制每一位中为1的个数和为0的个数,两者相乘即为总的不同的个数。
巧妙地把数组中两两数字的比较变化成了32位的二进制数字的比较。时间复杂度O(n)。
参考:http://www.cnblogs.com/grandyang/p/6208062.html
找规律:
4: 0 1 0 0
14: 1 1 1 0
2: 0 0 1 0
1: 0 0 0 1
我们先看最后一列,有三个0和一个1,那么它们之间相互的汉明距离就是3,即1和其他三个0分别的距离累加,然后在看第三列,累加汉明距离为4,因为每个1都会跟两个0产生两个汉明距离,同理第二列也是4,第一列是3。我们仔细观察累计汉明距离和0跟1的个数,我们可以发现其实就是0的个数乘以1的个数,发现了这个重要的规律,那么整道题就迎刃而解了,只要统计出每一位的1的个数即可。
Python代码:
class Solution(object):
def totalHammingDistance(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
for pos in range(32):
bitCount = 0
for i in range(len(nums)):
bitCount += (nums[i] >> pos) & 1
res += bitCount * (len(nums) - bitCount)
return res
二刷的时候选择C++,这次一眼就看出这个题的套路了:把每个位的1和0进行统计,这个位能够成的不同 = 1的个数×0的个数。累加每一位的不同即可。
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int res = 0;
for (int i = 0; i < 32; ++i) {
int count0 = 0, count1 = 0;
int mask = 1 << i;
for (int n : nums) {
if (n & mask) {
++count1;
} else {
++count0;
}
}
res += count0 * count1;
}
return res;
}
};
日期
2018 年 3 月 9 日
2019 年 2 月 26 日 —— 二月就要完了
【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)的更多相关文章
- [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】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- 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总的二进制距离
[抄题]: 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 ...
- LeetCode 461 Hamming Distance 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 461. Hamming Distance + 477. Total Hamming Distance
▶ 与 Hamming 距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...
随机推荐
- 苹果ios通过描述文件获取udid
苹果ios通过描述文件获取udid 需要准备的东西 1,安装描述文件只支持https的回调地址,所以需要申请https域名 2,描述文件签名,不安装也可,只要能接受红色的字 步骤: 1,准备xml文件 ...
- nuxt使用图片懒加载vue-lazyload
对于nuxt使用第三方插件的方式大体都是都是一致的,就是在plugins文件夹中新增插件对应的js文件进行配置与操作,然后在nuxt.config.js文件的plugins配置项中引入新建的js文件就 ...
- Flume(三)【进阶】
[toc] 一.Flume 数据传输流程 重要组件: 1)Channel选择器(ChannelSelector) ChannelSelector的作用就是选出Event将要被发往哪个Channel ...
- Oracle——生成Awr报告
Oracle--生成Awr报告 AWR的概念 Oracle数据库是一个使用量很多的数据库,关于Oracle数据库的性能.Oracle10g以后,Oracle提供了一个性能检测的工具:AWR(Autom ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- 【C++】结构体/结构体数组/结构体指针/结构体嵌套/函数参数/const
一.结构体声明 struct Student { //成员列表 string name; int age; int score; }; //s3;定义时直接声明 int main() { struct ...
- C#获取Windows10屏幕的缩放比例
现在1920x1080以上分辨率的高分屏电脑渐渐普及了.我们会在Windows的显示设置里看到缩放比例的设置.在Windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置.然而在默认情况下, ...
- ZooKeeper 06 - ZooKeeper 的常用命令
目录 1 - 服务端常用命令 2 - 客户端常用命令 3 - 常用四字命令 4 - ZooKeeper 日志的可视化 版权声明 若要部署 ZooKeeper 单机环境,请查看此篇:https://ww ...
- Tableau如何绘制双柱折线组合图2
一.数据源准备 二.创建计算字段月度-拖拽至列-右键-精确日期 CASE[指标] WHEN "同期"then DATETRUNC('day',[日期])-5 WHEN " ...
- 有了代码变更分解提交工具SmartCommit,再也不担心复合提交了
摘要:文将介绍一个代码提交辅助工具SmartCommit,其主要功能是通过杂糅变更分解算法自动生成分组提交方案,接受开发者的反馈和交互式调整,渐进式地引导和辅助开发者做出符合最佳实践的原子提交. 本文 ...