Total Hamming Distance(位运算 bit)
TheHamming distancebetween 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:
Note:
Elements of the given array are in the range of0 to
10^9
Length of the array will not exceed
10^4
.
分析
位运算,得到最大数字,然后while m<maxx。每一位bit都用m<<1然后 m# 。每个bit都 cnt0 * cnt1。不懂为什么*
cnt0 * cnt1 因为不一样的组合,比如2个0 3个1, 01的组合就是2*3=6 对
Last updated