why this code gives me wrong c++? any hint please


  • 0
    H

    ...
    class Solution {
    public:
    int threeSumClosest(vector<int>& nums, int target) {

        sort(nums.begin(),nums.end());
        int res=2147483647;
          
        for(int i=0;i<nums.size();i++)
        {
    
            if(i==0||nums[i]!=nums[i-1])
            {
    
                int l=i+1,r=nums.size();
                while(l<r)
                {
                	int tmp=nums[i]+nums[l]+nums[r];
                	 if(res==target)
                    	return res;
                    if(abs(tmp-target)<abs(res-target))
                    {
                        res=tmp;
                    }
                   
                    if((nums[i]+nums[l]+nums[r])>res)
                    r--;
                    else
                    l++;
                }
            }
        }
        return res;
    
    }
    

    };
    ...


Log in to reply
 

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