Why I can not pass the final test data?

  • -1

    First sort, then use the front pointer and rear point to make them meet.

    class Solution {
    int threeSumClosest(vector<int> &num, int target) {
    int gap=INT_MAX;
    int get1;
    int get2=INT_MAX;
    for(int i=1;i<num.size()-1;i++){

            int sum=target-num[i];
            int front=0;
            int rear=num.size()-1;
               if(front==i-1 && rear==i+1)break;
               if((get1>sum || front==i-1)&& rear!=i+1)rear--;
               if((get1<sum || rear==i+1)&& front!=i-1) front++;
               if(get1==sum)return target;
           if(abs(get1+num[i]-target)<abs(get2-target)){get2=get1+num[i];}//here here here here
        return get2;


    The problem is I can not pass the fianal test {-3 -2 -5 3 4} -1

    I run the code in visual studio, then I find the variable get2 never changes when it comes to the notified sentence although it really meets the if sentence.The sentence is used to store a new closer sum. But when I changed the beginning value of get2 to 10000000, the code pass the online judge. I don't now why...

    Please comment here! Thanks!

  • 0

    I've met the same problem before.
    I suggest you can set gap = num[0] + num[1] + num[2];

    If you set gap = INT_MAX, it might cause overflow.

Log in to reply

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