【LeetCode】477. Total Hamming Distance 解题报告(Python C++)

news/2024/10/23 9:26:49/

作者: 负雪明烛
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, 2Output: 6Explanation: 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 014:    1 1 1 02:     0 0 1 01:     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 = 0for pos in range(32):bitCount = 0for i in range(len(nums)):bitCount += (nums[i] >> pos) & 1res += 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 日 —— 二月就要完了


http://www.ppmy.cn/news/535629.html

相关文章

【Leetcode】477. Total Hamming Distance

方法一&#xff1a; 思路&#xff1a; (1)遍历数组nums&#xff0c;对每一个nums[i]&#xff0c;求其余后面的每一个nums[j]的Hamming Distance。 (2)求x与y的Hamming Distance的方法&#xff1a; ---1)先求x^y的结果res。 ---2)再依次求32位res的每一位与1进行与操作的结…

Codeforces Round #477 C. Stairs and Elevators

Codeforces Round #477 C. Stairs and Elevators 题目链接 题意&#xff1a;给你一栋高n层&#xff0c;每一层由m个部分组成&#xff0c;可以看成是一个矩阵划分成行和列。给你一cl,ce分别表示有个楼梯和电梯&#xff0c;然后给你一个v表示电梯的速度。接下来行表示每个电梯…

[477]tf.reduce_mean()

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴&#xff08;tensor的某一维度&#xff09;上的的平均值&#xff0c;主要用作降维或者计算tensor&#xff08;图像&#xff09;的平均值。 reduce_mean(input_tensor,axisNone,keep_dimsFalse,nameNone,reduction_indices…

LeetCode_位运算_中等_477.汉明距离总和

目录 1.题目2.思路3.代码实现&#xff08;Java&#xff09; 1.题目 两个整数的汉明距离指的是这两个数字的二进制数对应位不同的数量。 给你一个整数数组 nums&#xff0c;请你计算并返回 nums 中任意两个数之间汉明距离的总和。 示例 1&#xff1a; 输入&#xff1a;nums …

LeetCode | 477. Total Hamming Distance

题目 The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given an integer array nums, return the sum of Hamming distances between all the pairs of the integers in nums. Example 1: Input: n…

【博客477】prometheus-----数值数据编码(varint与zigzag)

prometheus-----数据编码(varint与zigzag) prometheus对数值数据进行编码时&#xff0c;使用到了varint与zigzag varint与zigzag编码方法在protobuf中也被使用 prometheus encoding代码&#xff1a; https://github.com/prometheus/prometheus/blob/main/tsdb/encoding/encodi…

​LeetCode刷题实战477:汉明距离总和

算法的重要性&#xff0c;我就不多说了吧&#xff0c;想去大厂&#xff0c;就必须要经过基础知识和业务逻辑面试算法面试。所以&#xff0c;为了提高大家的算法能力&#xff0c;这个公众号后续每天带大家做一道算法题&#xff0c;题目就从LeetCode上面选 &#xff01; 今天和大…

LeetCode 算法 每日一题 477.汉明距离总和

10.正则表达式匹配 题目描述 两个整数的汉明距离指的是这两个数字的二进制数对应位不同的数量。 计算一个数组中&#xff0c;任意两个数之间汉明距离的总和。 示例1 输入: 4, 14, 2 输出: 6 解释: 在二进制表示中&#xff0c;4表示为0100&#xff0c;14表示为1110&#xff0…