Output Limit Exceeded


  • 0
    S

    Commit and get "Output Limit Exceeded" result. In fact, i thought that duplicate triplets have been deplicated . So confused and has no idea what happened. Below was my solution code and test code for this problem.
    Algorithm idea is easy to understand, just use the core idea of twosum. Time Complexity can be O(n^2).
    And some test cases passed like:

    • 6 -1 0 1 2 -1 -4 [first number represent size of vector]
    • 4 0 0 0 0
    • 6 -1 -1 -1 0 1 1
       #include <iostream>
        #include <fstream>
        #include <vector>
        #include <cstdlib>
        #include <algorithm>
        using namespace std;
        
        
        
        class Solution{
        	public:
        		vector<vector<int> > threeSum(vector<int>  &num){
        			vector<vector<int> >rst;
        			//origin vector changed in this function
        			sort(num.begin(),num.end());
        
        			typedef vector<int>::iterator VIter_t;
        			int tsum=-1;
        			for(VIter_t iter=num.begin();iter!=num.end(); ++iter){
        			
        				int ttemp=-(*iter);
        				if(ttemp==tsum) //ignore the same element and avoid duplicate solution
        					continue;
        				tsum=ttemp;
        				/*do as the 'two sum way' , but scan to find all soluttion(may more than one)*/	
        				for(VIter_t hiter=iter+1,titer=num.end()-1;
        						titer-hiter>0;){
        					if((*hiter)+(*titer)<tsum)
        						hiter++;
        					else if(*hiter+*titer==tsum){
        						vector<int> *v=new vector<int>();
        						v->push_back(*iter);
        						v->push_back(*hiter);
        						v->push_back(*titer);
        						
        						rst.push_back(*v);		
        						
        						hiter++; titer--;
        					}
        					else 
        						titer--;
        						
        				}
        			}
        			return rst;
        			
        		}
        };
        /***************test code*********************/
        template<class T>
        void printElement(T elt){
        	for(typename T::iterator iter=elt.begin();iter!=elt.end(); ++iter)
        		cout<<*iter<<" ";
        	
        }
        template<class T>
        void printOut(T list){
        	cout<<"element size:"<<list.size()<<endl;
        	typename T::iterator iter=list.begin();
        	for(;iter!=list.end();++iter){
        		printElement(*iter);
        		cout<<endl;	
        	}
        }
        // typedef vector<vector<int> > VVlist;
        // ostream& (ostream &cout,VVlist& ct){
        // 	printOut<VVlist>(ct);
        // }
        
        /*function main to debug*/
        int main(int argc,char **argv){
        #ifdef _mydebug
        #define dinfo "read data specified file\n"
        	ifstream cin("input");
        #else
        #define dinfo ""
        #endif
        	cout<<dinfo;
        
        	int count;
        	while(cin>>count){
        		vector<int> prices;
        		int onev;
        		for(int i=0;i<count;++i){
        			cin>>onev;
        			prices.push_back(onev);
        		}
        		Solution slt;
        		// 	int maxp=slt.maxPorofit(prices);
        		printOut<vector<vector<int> > >(slt.threeSum(prices));
        		cout<<endl;
        		std::cout<<"----------------------"<<endl;
        	}
        	return 0;
        }

Log in to reply
 

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