https://leetcode.com/problems/total-hamming-distance/description/
int totalHammingDistance(vector<int>& nums) {
if(nums.size()<2) return 0;
int N = nums.size();
int res = 0, w=1;
for(int i=0; i<32; i++) {
int n1 = 0;
for(int j=0; j<N; j++) {
n1 += (nums[j]&w)!=0;
}
res += n1*(N-n1);
w <<= 1;
}
return res;
}
No comments:
Post a Comment