concise c++


  • 0
    S
    class Solution {
    public:
        int totalHammingDistance(vector<int>& nums) {
            if(nums.empty())
                return 0;
    
            array<int, 32> oneBitCounts{0};
            for(int i : nums)
            {
                for(int bitNum = 0; i; ++bitNum)
                {
                    oneBitCounts[bitNum] += (i & 1);
                    i >>= 1;
                }
            }
            
            int distance = 0;
            for(int i : oneBitCounts)
                distance += ((nums.size() - i) * i);
            
            return distance;
        }
    };
    

Log in to reply
 

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