C++ Bit Manipulation Solution using O(n) time and O(1) space


  • 0
    Y
    class Solution {
    public:
        int totalHammingDistance(vector<int>& nums) {
            int res = 0;
            if(nums.size() == 0)
                return res;
            int maxnum = 0, mask = 1;
            for(int i = 0; i < nums.size(); ++ i)
                maxnum = max(maxnum, nums[i]);
            while(mask <= maxnum){
                res += getHammingDistPair(nums, mask);
                mask <<= 1;
            }
            return res;
        }
    private:
        inline int getHammingDistPair(vector<int>& nums, int mask){
            int cnt[2] = {0};
            for(int i = 0; i < nums.size(); ++ i){
                ++ cnt[((mask&nums[i]) == 0?0:1)];
            }
            return cnt[0]*cnt[1];
        }
    };
    

Log in to reply
 

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