作者: 负雪明烛
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:

  1. Elements of the given array are in the range of 0 to 10^9
  2. 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++)的更多相关文章

  1. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] 477. Total Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  3. 【LeetCode】461. Hamming Distance 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...

  4. LeetCode "477. Total Hamming Distance"

    Fun one.. the punch line of this problem is quite common in Bit related problems on HackerRank - vis ...

  5. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  6. 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 ...

  7. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  8. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  9. 461. Hamming Distance + 477. Total Hamming Distance

    ▶ 与 Hamming  距离相关的两道题. ▶ 461. 求两个数 x 与 y 的哈夫曼距离. ● 代码,4 ms,对 x 和 y 使用异或,然后求值为 1 的位的个数. class Solutio ...

随机推荐

  1. EXCEL-时间

    1.时间的3中输入方法: (1)手打输入: (2)快捷键:[快,方便,且结果跟手打一样,不会改变][Ctrl+;]->年月日    [Ctrl+:]->时分秒(我这里是这样的效果) (3) ...

  2. 一款真正可以拿的出手的本土嵌入式RTOS-SylixOS

    由 winniewei 提交于 周四, 12/20/2018 作者:张国斌 在参加工信部人才交流中心和南京浦口区开发区管委会联合举办的第三届集成电路产业紧缺人才创新发展高级研修班暨产业促进交流会期间, ...

  3. C++ STL算法之:copy

    C++ STL算法:copy 目录(?)[+] 前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法.所谓变易算法(Mutating a ...

  4. 添加页面、页面交互、动态添加页面tab

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ViewDictTosPr ...

  5. InnoDB学习(一)之BufferPool

    我们知道InnoDB数据库的数据是持久化在磁盘上的,而磁盘的IO速度很慢,如果每次数据库访问都直接访问磁盘,显然严重影响数据库的性能.为了提升数据库的访问性能,InnoDB为数据库的数据增加了内存缓存 ...

  6. 紧张 + 刺激,源自一次 OOM 历险

    作者 | 蚂蝗 背景 ​ Erda 是集 DevOps.微服务治理.多云管理以及快数据管理等多功能的开源一站式企业数字化平台.其中,在 DevOps 模块中,不仅有 CI/CD.项目协同等功能,同时还 ...

  7. 零基础学习java------day12------数组高级(选择排序,冒泡排序,二分查找),API(Arrays工具类,包装类,BigInteger等数据类型,Math包)

    0.数组高级 (1)选择排序 它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置 ...

  8. nodejs-CommonJS规范

    JavaScript 标准参考教程(alpha) 草稿二:Node.js CommonJS规范 GitHub TOP CommonJS规范 来自<JavaScript 标准参考教程(alpha) ...

  9. oracle 执行计划的获取方法

    1.用explain plan for来获取执行计划 explain plan for <sql>; select * from table(dbms_xplan.display()); ...

  10. Android 实现微信QQ分享以及第三方登录

    集成准备 在微信开放平台创建移动应用,输入应用的信息,包括移动应用名称,移动应用简介,移动应用图片信息,点击下一步,选择Android 应用,填写信息提交审核. 获取Appkey 集成[友盟+]SDK ...