Why this solution is "output limit exceeded"?


  • 0
    R
    class Solution {
    public:
    void reverseWords(string &s) {
    	list <int> pipeline;
     	int i=0;
    	int last=0;
    	int start;
    	int end;
    	int temp=0;
    	bool flag=true;
    	bool inWord=false;
    	char key;
    
    	while(((key=s[i])!='\0')&&(s.length()>1)){
    		while((key==' '))
    		{   
    			if(inWord)
    			{pipeline.push_back(i-1);}
    			i++;
    			inWord=false;
    			key=s[i];
    		}
    		while((key!=' ')&&(key!='\0'))
    		{   
    		    if(!inWord)
    			{pipeline.push_back(i);inWord=true;}
    			i++;
    			key=s[i];
    		}
    		if((key=='\0')&&(inWord))
    		{
    			pipeline.push_back(i);
    		}
    	}
    
    	while(pipeline.size()>0){
    	    end=pipeline.back();
    	    pipeline.pop_back();
    		 start=pipeline.back();
    	    pipeline.pop_back();
    		if((start==0)||(end==0))
    			flag=false;
    		cout<<s.substr(start,end-start+1)<<' ';
    		if(!flag)
    			break;
    
    	}
    }
    

    };


Log in to reply
 

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