参考:
https://leetcode-cn.com/problems/total-hamming-distance/solution/guan-wang-jie-fa-an-wei-zhao-ju-chi-ran-hou-jia-qi/
python思路:补足前面的位数,然后计算每一位0和1的数量且相乘后相加。
class Solution:
def totalHammingDistance(self, nums) -> int:
nums_len = len(nums)
if nums_len == 0:
return 0
bin_nums = [bin(num)[2:] for num in nums]
max_len = max([len(i) for i in bin_nums])
# 补足数字小的二进制前面部分的0
bin_nums = ['0' * (max_len - len(i)) + i for i in bin_nums]
sum = 0
for i in range(max_len):
zero_count = [x[i] for x in bin_nums].count('0')
sum += zero_count * (nums_len - zero_count)
return sum
if __name__ == '__main__':
x = Solution()
print(x[2,14,4])
```
package T477汉明距离总和;
public class Solution {
public int totalHammingDistance(int[] nums) {
int[] cnt = new int[32];
for (int i = 0; i < 32; i++) {
for (int val : nums) {
if ((val & (1<<i)) != 0) {
cnt[i]++;
}
}
}
int total = 0;
for (int i = 0; i < 32; i++) {
total += (cnt[i]*(nums.length - cnt[i]));
}
return total;
}
}