Why this code is Output Limit Exceeded?


  • 0
    R

    After I finished this topic, I have tested several sentences including an empty string. But, this solution still cannot pass the test. It shows a Output Limit Exceeded.

    How can I fix this problem?

    class Solution {
    public:
    void reverseWords(string &s) {
    	list <int> pipeline;
     	int i=0;
    	int start;
    	int end;
    	bool flag=true;
    	bool inWord=false;
    	char key;
        if(s.length()<1) return;
    	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;
               }
               }
              }

  • 0
    S
    This post is deleted!

  • 0
    S

Log in to reply
 

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