Why Output Limit Exceeded???Please help


  • 0
    L
    class Solution {
    public:
        vector<vector<int> > threeSum(vector<int> &num) {
            vector<vector<int> > res;
            res.clear();
            if(num.size()<=2) return res;
            sort(num.begin(),num.end());
            for (int i=0;i<num.size()-2;i++){
                if (i>0 && num[i]==num[i-1]) continue;
                int j = i+1,k = num.size()-1;
                while(j<k){
                    if(num[i]+num[j]+num[k]>0){
                        --k;
                    }
                    else if(num[i]+num[j]+num[k]<0){
                        ++j;
                    }
                    else{
                        vector<int> tmp;
                        tmp.push_back(num[i]);
                        tmp.push_back(num[j]);
                        tmp.push_back(num[k]);
                        res.push_back(tmp);
    					--k;++j;
                    }
                }
            }
            return res;
        }
    };

  • 0
    L
    This post is deleted!

  • 0
    O

    Because the way you de-dup is not correct. Seems OJ will check the length of output first before actually goes into it or something. So the output size is not as expect, it raised this OLE exception.
    Consider taking a test case like {-1,0,1,2,-1,-4,-1,2,0}


Log in to reply
 

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