C++ solution


  • 0
        int totalHammingDistance(vector<int>& nums) {
        	int totalHammingDistance = 0;
        	for (int i = 0; i < 31; i++) {
        		int zero = 0, one = 0;
        		for (auto num : nums) {
        			if (num >> i & 1)
        				one++;
        			else
        				zero++;
        		}
        		totalHammingDistance += one * zero;
        	}
        	return totalHammingDistance;
        }
    

Log in to reply
 

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