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.

help "output limit exceeded"

0 votes
74 views

I have noticed the duplicated problem, why it still not work? Could some one help me?

    vector<vector<int> > threeSum(vector<int> &num) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    vector<vector<int> > re;//store the result
    vector<int> v;
    map<int,int>m;// put num into map.
    for(int i=0;i<num.size();i++){
        if(m.find(num[i])==m.end())m[num[i]]=1;
        else m[num[i]]=m[num[i]]+1;// eg if 1 appear twice, then m[1]=2
    }
    sort(num.begin(),num.end());//sort num
    for(int i=0;i<num.size();i++)
        for(int j=i+1;j<num.size();j++)
        {  //num[i] is the smallest,num[j] is the middle,-num[i]-num[j] is the largest
           //num[i] is the smallest,so it must smaller than 0
            if(num[i]>0)break;
            v.clear();
            m[num[i]]--;
            m[num[j]]--;
            if(m.find(-num[i]-num[j])!=m.end()&&m.find(-num[i]-num[j])->second>0&&(-num[i]-num[j])>=num[j])
              {v.push_back(num[i]);v.push_back(num[j]);v.push_back(-num[i]-num[j]);re.push_back(v);
              while(i<num.size()-1&&num[i]==num[i+1])i++;}
            m[num[i]]++;
            m[num[j]]++;
        }
    return re;

}
asked Nov 26 in 3Sum by wabcidf (270 points)

Could you briefly describe your approach on how you remove the duplicates from the answer? Edit your question and describe your approach, do not leave it as a comment/answer.

1 Answer

0 votes
 
Best answer

See comments

vector<vector<int> > threeSum(vector<int> &num) {
    vector<vector<int> > re;
    vector<int> v;
    map<int,int>m;
    for(int i=0;i<num.size();i++) {
        if(m.find(num[i])==m.end())m[num[i]]=1;
        else m[num[i]]=m[num[i]]+1;
    }
    sort(num.begin(),num.end());
    for(int i=0;i<num.size();i++) {
        if(i>0 && num[i] == num[i-1]) continue; // Skip the same num[i] here.
        if(num[i]>0) break; // Break if num[i] > 0
        for(int j=i+1;j<num.size();j++) { 
            if(j>i+1 && num[j] == num[j-1]) continue; // Skip the same num[j] here.
            if(-num[i]-num[j]<num[j]) break; // Break if -num[i]-num[j] is smaller than num[j]
                                             // That means no possible answer afterwards.
            v.clear();
            m[num[i]]--;
            m[num[j]]--;
            if(m.find(-num[i]-num[j])!=m.end()&&m.find(-num[i]-num[j])->second>0) {
                v.push_back(num[i]);
                v.push_back(num[j]);
                v.push_back(-num[i]-num[j]);
                re.push_back(v);
            }
            m[num[i]]++;
            m[num[j]]++;
        }
    }
    return re;
}
answered Nov 26 by porker2008 (5,780 points)
selected Nov 26 by wabcidf

Thank you a lot~ Now I know where I am wrong.


...