passes 37 out of the 47 test cases ! and errors ! does anyone have a clue how to fix it ?? c++


  • 0
    Y

    '''
    class Solution {
    public:
    int totalHammingDistance(vector<int>& nums) {
    /* we should go through all the pairs of elements in nums and caculate the distance between each two and then we sum up all the sums to find the total hamming distance */

        if(nums.size()==1 || nums.size()==0){
            return 0;
        }
        
        int global_distance=0;
        for(std::vector<int>::iterator it1=nums.begin(); it1!=nums.end()-1; ++it1){
            for(std::vector<int>::iterator it2=it1+1; it2!=nums.end(); ++it2){
                
                /* xor both elements*/
                int x= *it1^*it2;
                int local_distance = 0;
                while(x!=0){
                    if(x&1==1){
                        local_distance+=1;
                    }
                    x=x>>1;
                }
                
                global_distance += local_distance;
            }
            
        }
    
    return global_distance;
    
    }
    

    };
    '''


Log in to reply
 

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