Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

why output limit exceeded in this code

0 votes
358 views
class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        vector<vector<int>>v;
        int i,j,t,k,temp;
        sort(num.begin(),num.end());
        vector<int>b;
        int n=num.size();
        for(i=0;i<n;)
        {
            t=-1*num[i];
            j=i+1;
            k=n-1;
            while(k>j)
            {
                int temp=num[k]+num[j];
                if(temp==t)
                {
                    b.push_back(num[i]);
                    b.push_back(num[j]);
                    b.push_back(num[k]);
                    v.push_back(b);
                    j++;
                    k--;
                    b.clear();
                }
                else if(temp<t)
                {
                    j++;
                }
                else
                k--;
            }
            t=num[i];
            while(t==num[i])
            i++;
        }
        return v;
    }
};
asked Dec 12, 2013 in 3Sum by ANMOLDHURIA (260 points)
edited Dec 12, 2013 by ANMOLDHURIA

Could you please update your post with the explanation of your algorithm and add comment in your code? Thanks.

Thanks for your reply. My problem is solved now. Actually,I forget to skip same numbers that caused output limit exceeded. My algorithm is to select one number. and use the concept of 2 sum problem.

leetcode must update recently, because it can be accepted without skip the same number before.

Yes, the judge is stricter now and some sub-optimal solutions were prevented from passing the judge.

Please log in or register to answer this question.


...