Extremely simple C++ solution, beats 99.5% of solutions (52ms)

  • 0

    Should be pretty simple, go through the nth bit of each number, count the number of ones, and add to the return value the (number of ones)*(number of zeros)

    class Solution {
        int totalHammingDistance(vector<int>& nums) 
            int ret = 0;
            int s = nums.size();
            for(int i=0;i<30;i++)
                int ones = 0;
                for(int j=0;j<s;j++)
                ret+= (ones*(s-ones));
            return ret;

Log in to reply

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