【Leetcode】477. Total Hamming Distance

news/2024/10/23 9:33:03/

方法一:

思路:

(1)遍历数组nums,对每一个nums[i],求其余后面的每一个nums[j]的Hamming Distance。

(2)求x与y的Hamming Distance的方法:

---1)先求x^y的结果res。

---2)再依次求32位res的每一位与1进行与操作的结果,若不为0,则Hamming Distance加一。

public class Solution {public int totalHammingDistance(int[] nums) {int len = nums.length;if (len == 0)return 0;int result = 0;for (int i = 0; i < len - 1; i++) {for (int j = i + 1; j < len; j++)result += hammingDistance(nums[i], nums[j]);}return result;}public int hammingDistance(int x, int y) {int res = x ^ y;int count = 0;for (int i = 0; i < 32; i++) {if ((res & 1) != 0)count++;res >>= 1;}return count;}
}

超时!!!


方法二:

思路:

(1)设置变量oneCount记录某一位上1的个数,result记录总的Hamming Distance。

(2)依次判断32位中的每一位。每判断完一位,右移一位继续判断下一位。

(3)针对每一位,遍历nums数组,判断每一个nums[j]的该位是否为1(nums[j]与1进行与操作得到的结果不是0则nums[j]的该位为1,oneCount加一)。

(4)遍历完nums数组后,oneCount * (len - oneCount)即为该位上的Hamming Distance,加到总结果result中。

public class Solution {public int totalHammingDistance(int[] nums) {int result = 0, oneCount = 0, len = nums.length;for (int i = 0; i < 32; i++) {oneCount = 0;for (int j = 0; j < len; j++) {if ((nums[j] & 1) != 0)oneCount++;nums[j] >>= 1;}result += (oneCount * (len - oneCount));}return result;}
}

Runtime:45ms

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

相关文章

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…

奇舞周刊 477 期:一文弄懂 React ref 原理

记得点击文章末尾的“ 阅读原文 ”查看哟~ 下面先一起看下本期周刊 摘要 吧~ 奇舞推荐 ■ ■ ■ 一文弄懂 React ref 原理 对于 Ref 理解与使用&#xff0c;一些读者可能还停留在用 ref 获取真实 DOM 元素和获取类组件实例层面上 其实 ref 除了这两项常用功能之外&#xff0c;还…