leetcode 477. Total Hamming Distance

本文介绍了一种高效计算一组整数间所有两两配对的汉明距离总和的方法。通过遍历32位整数的每一位,统计该位上为1的数量,进而计算出该位对总汉明距离的贡献。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 to 10^9
  2. Length of the array will not exceed 10^4.
我的第一个想法就是循环每一对数,求出每对的(x^y)中的'1'的,但是as you can imagine, it TLE...

用另外的思路来解决。32bit的整数,我们每次只看其中的1位。
比如,只看 nums 数组中所有数字的rightmost bit. 如果有 i 个数字的rightmost bit是 0,有j 个数字的rightmost bit是 1,那么 i * j将会是这一位上汉明距离增加的值。
如:0100
       1110
       0010
       0110          
       1010
看最左一位,有3个数字中这一位是0,有2个数字中这一位是1。0 0 0 1 1,对于其中一个这一位是0的数,它要跟另外2个这一位是1的数配对,所以对于这一位,汉明距离要+2。其他这一位是0的数同理,汉明距离都要+2。因此结果是2+2+2,也就是2*3。

public int totalHammingDistance(int[] nums) {
		int n=nums.length;
		int total=0;
		for(int i=0;i<32;i++){
			int countOf_1=0;
			for(int j=0;j<n;j++){
				countOf_1 += ( (nums[j]>>i) & 1 );
			}
			total+=countOf_1*(n-countOf_1);
		}
		return total;
}

For each bit position 1-32 in a 32-bit integer, we count the number of integers in the array which have that bit set. Then, if there are n integers in the array and k of them have a particular bit set and (n-k) do not, then that bit contributes k*(n-k) hamming distance to the total.

参考资料:https://stackoverflow.com/questions/21388448/sum-of-xor-values-of-all-pairs

因为stackoverflow经常打不开或者打开非常慢,因此我直接复制在下面。

sum of xor values of all pairs

We have an array A (say [1,2,3]) . We need to find the XOR(^)SUM of all pairs of integers in the array. Though this can easily be done in O(n^2) but how can i improve the complexity of the solution ? E.g for the above array , A, the answer would be (1^2)+(1^3)+(2^3) = 6 Thanks.

share improve this question
 
 
By "sum" I am assuming you mean bitwise or. –  Matt  Jan 27 '14 at 18:20
 
NO. Xor all the distinct pairs and calculate the sum of all these values. –  pranay  Jan 27 '14 at 18:26
 
In that case I get 1^2+1^3+2^3 = 3 + 2 + 1 = 6. –  Matt  Jan 27 '14 at 18:30
 
Thanks Matt..question updated. –  pranay  Jan 27 '14 at 18:38
1  
What should the answer be for [1,2,2]? Is it (1^2)+(1^2)=6, or 1^2=3? If it's the first, then it is unnecessary to specify "distinct pairs" in the question since 2^2=0 anyway and wouldn't contribute to the sum. –  Matt  Jan 27 '14 at 18:44 

3 Answers

up vote 11 down vote accepted

You can separate the calculation to do one bit at a time.

For example, look at the rightmost bit of all the numbers in the array. Suppose that a numbers have a rightmost 0-bit, and b numbers have a 1-bit. Then out of the pairs, a*b of them will have 1 in the rightmost bit of the XOR. This is because there are a*b ways to choose one number that has a 0-bit and one that has a 1-bit. These bits will therefore contribute a*b towards the total of all the XORs.

In general, when looking at the nth bit (where the rightmost bit is the 0th), count how many numbers have 0 (call this an) and how many have 1 (call this bn). The contribution towards the final sum will be an*bn*2n. You need to do this for each bit and sum all these contributions together.

This can be done in O(kn) time, where k is the number of bits in the given values.

share improve this answer
 
 
@MooingDuck For that set, my algorithm gives 2*2*pow(2,0) + 1*3*pow(2,1) = 10, which is the correct answer. I don't know what you mean by "the 3 participates more than the two" or why you think this is incorrect. –  interjay  Jan 27 '14 at 19:08 
 
Great idea Interjay! Thanks a lot :) –  pranay  Jan 27 '14 at 19:14
 
@MooingDuck The correct answer is 10 decimal: (1^2)+(1^2)+(1^3)+(2^2)+(2^3)+(2^3) == 10. –  interjay  Jan 27 '14 at 19:20
 
Wait, right. Of course >.< –  Mooing Duck  Jan 27 '14 at 19:35

内容概要:本文详细介绍了扫描单分子定位显微镜(scanSMLM)技术及其在三维超分辨体积成像中的应用。scanSMLM通过电调透镜(ETL)实现快速轴向扫描,结合4f检测系统将不同焦平面的荧光信号聚焦到固定成像面,从而实现快速、大视场的三维超分辨成像。文章不仅涵盖了系统硬件的设计与实现,还提供了详细的软件代码实现,包括ETL控制、3D样本模拟、体积扫描、单分子定位、3D重建和分子聚类分析等功能。此外,文章还比较了循环扫描与常规扫描模式,展示了前者在光漂白效应上的优势,并通过荧光珠校准、肌动蛋白丝、线粒体网络和流感A病毒血凝素(HA)蛋白聚类的三维成像实验,验证了系统的性能和应用潜力。最后,文章深入探讨了HA蛋白聚类与病毒感染的关系,模拟了24小时内HA聚类的动态变化,提供了从分子到细胞尺度的多尺度分析能力。 适合人群:具备生物学、物理学或工程学背景,对超分辨显微成像技术感兴趣的科研人员,尤其是从事细胞生物学、病毒学或光学成像研究的科学家和技术人员。 使用场景及目标:①理解和掌握scanSMLM技术的工作原理及其在三维超分辨成像中的应用;②学习如何通过Python代码实现完整的scanSMLM系统,包括硬件控制、图像采集、3D重建和数据分析;③应用于单分子水平研究细胞内结构和动态过程,如病毒入侵机制、蛋白质聚类等。 其他说明:本文提供的代码不仅实现了scanSMLM系统的完整工作流程,还涵盖了多种超分辨成像技术的模拟和比较,如STED、GSDIM等。此外,文章还强调了系统在硬件改动小、成像速度快等方面的优势,为研究人员提供了从理论到实践的全面指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值