C++ solution in 8 lines


  • 0
    P
    int totalHammingDistance(vector<int>& nums) {
        int ans = 0, tester = 1;
        for (int i = 0; i < 32; ++i) {
            int count = 0;
            for (auto n : nums) if ((n & tester) == 0) count++;
            ans += (count * (nums.size() - count));
            tester <<= 1;
        }
        return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.