Run code ok,but submit wrong.why?


  • 0
    T
    int arrayPairSum(int* nums, int numsSize) {
        int max=nums[0];
        int min=nums[0];
        for(int i=1;i<numsSize;++i)
        {
            max<nums[i]?max=nums[i]:max,(min>nums[i]?min=nums[i]:min);
        }
        
        int* flg=(int*)malloc(sizeof(int)*(max-min+1));
        memset(flg,0,max-min+1);
        int ret=0;
        bool f=true;
        
        for(int i=0;i<numsSize;++i)
        {
            flg[nums[i]-min]++;
        }
       
        for(int i=0;i<max-min+1;++i)
        {
            while(flg[i]>0)
            {
                --flg[i];
                if(f)
                    ret+=(i+min);
                f=!f;
                
            }
        }
        free(flg);
        return ret;
    }
    

Log in to reply
 

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