Could not pass test 116


  • 0
    Y

    I think my code is OK, but on test 116 it outputs 1 less than expected. Really appreciate if someone could point out my problem.

    class Solution {
    public:
    int threeSumClosest(vector<int>& nums, int target) {
        int minGap=INT_MAX;
        int diff=0;
        sort(nums.begin(),nums.end());
        int p1,p2;
        for(int i=0;i<nums.size()-2;++i){
            p1 = i+1;
            p2= nums.size() - 1; 
            while(p1<p2){
                if(abs(nums[i]+nums[p1]+nums[p2]-target)<minGap){
                    diff=nums[i]+nums[p1]+nums[p2]-target;
                    minGap = abs(nums[i]+nums[p1]+nums[p2]-target);
                }
                if(diff<0){
                    p1++;
                }else {
                    p2--;
                }
            }
            
        }
            return diff+target;
            
        }
    

    };


Log in to reply
 

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