Skip to content

Instantly share code, notes, and snippets.

@jecyhw
Created December 18, 2016 03:16
Show Gist options
  • Select an option

  • Save jecyhw/d27fec696342dd085abd466ac84562b9 to your computer and use it in GitHub Desktop.

Select an option

Save jecyhw/d27fec696342dd085abd466ac84562b9 to your computer and use it in GitHub Desktop.
LeetCode Weekly Contest 13
class Solution {
public:
struct node {
int z, o;
node() {
z = o = 0;
}
};
int totalHammingDistance(vector<int>& nums) {
vector<node> v(32);
for (auto num : nums) {
for (int i = 0; i < v.size(); ++i) {
if (num & 1) {
++v[i].o;
} else {
++v[i].z;
}
num >>= 1;
}
}
int ans = 0;
for (auto n : v) {
ans += n.o * n.z;
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment