当前位置: 代码迷 >> 综合 >> LeetCode 477. Total Hamming Distance
  详细解决方案

LeetCode 477. Total Hamming Distance

热度:12   发布时间:2023-12-21 12:39:47.0

题目链接:https://leetcode.com/problems/total-hamming-distance/description/
题目大意:给n个数,求所有Hamming距离之和

不知道说啥。。。。。
算是矮子里面拔将军吧。。。。(其实是见识少)
不过也就是初中思维(逃)
以后还是多找点有趣的例子。。。。

class Solution {
public:int totalHammingDistance(vector<int>& nums) {int total = 0;int size = nums.size();for (int bit = 0; bit < 32; ++bit) {int bitCount = 0;for (int i = 0; i < size; ++i)bitCount += (nums[i] >> bit) & 1;total += bitCount * (size - bitCount);}return total;}
};
  相关解决方案