O(n) c++


  • 0
    K
    class Solution {
    public:
        int totalHammingDistance(vector<int>& nums) {
            int res = 0, n = nums.size(), cur = 1;
            if (n < 2) return 0;
            while (cur) {
                int ones = 0, zeros = 0;
                for (auto &num : nums) {
                    if (num & cur) ++ones;
                    else ++zeros;
                }
                res += ones * zeros;
                cur <<= 1; 
            }
            return res;
        }
    };
    

Log in to reply
 

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