【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 ...
随机推荐
- ubuntu终端颜色快速配置
ubuntu终端颜色快速配置 根据以下step步骤设置即可 step1:备份:cp ~/.bashrc ~/.bashrc.backup step2:打开文件:vim ~/.bashrc step3: ...
- pow()是如何实现的?
如1.5 ** 2.5,如何计算?似乎是这样的: 1. cmath calculates pow(a,b) by performing exp(b * log(a)). stackoverflow 2 ...
- ASP.NET Core中使用固定窗口限流
算法原理 固定窗口算法又称计数器算法,是一种简单的限流算法.在单位时间内设定一个阈值和一个计数值,每收到一个请求则计数值加一,如果计数值超过阈值则触发限流,如果达不到则请求正常处理,进入下一个单位时间 ...
- 【leetcode】212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board. Each ...
- 【spring AOP】@Pointcut的12种用法
@Pointcut用来标注在方法上来定义切入点. 使用格式:@ 注解(value="表达标签 (表达式格式)").如:@Pointcut("execution(* com ...
- spring的核心容器ApplicationContext
//bean.xml配置文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=" ...
- 【C/C++】日期问题/算法笔记/入门模拟
最近把算法竞赛入门经典的前半部分看完了,开始看算法笔记入门算法. 看了前半部分的例题,很多是算法竞赛入门经典中出现过的,但是感觉这本书写的更适合初学者,而且真的很像考试笔记,通俗易懂. //日期问题 ...
- Apifox(2)快速上手apifox
快速上手 使用场景 Apifox 是接口管理.开发.测试全流程集成工具,使用受众为整个研发技术团队,主要使用者为前端开发.后端开发和测试人员. 前端开发 接口文档管理 接口数据 Mock 接口调试 前 ...
- 00 - Vue3 UI Framework - 阅读辅助列表
阅读列表 01 - Vue3 UI Framework - 开始 02 - Vue3 UI Framework - 顶部边栏 03 - Vue3 UI Framework - 首页 04 - Vue3 ...
- Mybatis-plus报Invalid bound statement (not found)错误
错误信息 org.springframework.security.authentication.InternalAuthenticationServiceException: Invalid bou ...