Help "output limit exceeded"


  • 0
    W

    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;
        
    }

  • 0

    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.


  • 0
    P

    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;
    }

  • 0
    W

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


Log in to reply
 

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