I got a problem of "Output Limit Exceeded" and I want your help.


  • 0
    L
    void reversewords(string &s)
    {
    	int length = s.size();
    	if (0 == length)                  
    	{
    		return;
    	}
    	int i = length-1;
    	while (i >= 0)
    	{
    		if (i >= 0 && ' '== s[i])
    		{
    			i--;
    			continue;
    		}
    		int location = (s.substr(0,i+1)).rfind(' ');
    		string str = s.substr(location+1,i-location+1);
    		i = i-str.size();
    		if (i > 0)
    		{
    			cout<<str<<" ";
    		}
    		else
    		{
    			cout<<str<<endl;
    		}
    	}
    }
    
    
    
    void reversewords(string &s)
    {
    	const int max = 100;               
    	char* stack = new char[max];
    	int top = -1; 
    	int length = s.size();
    	if (0 == length)                  
    	{
    		return;
    	}
    	int i = length-1;
    	while (i >= 0)
    	{
    		int tmp = i;
    		while (i >= 0 && ' ' == s[i])       
    		{
    			i--;
    		}
    		if (top > -1 && tmp != i) 
    		{
    			while (top >=0 )
    			{
    				cout<<stack[top];
    				top--;
    			}
    			if (i != -1)
    			{
    				cout<<" ";
    			}
    			else
    			{
    				cout<<endl;
    			}
    		}
    		if (i >= 0 ) 
    		{
    			stack[++top] = s[i];
    			i--;
    		}
    	}
    }

  • 0
    S

    Read https://oj.leetcode.com/faq/, pay attention to this question Am I allowed to print something to stdout?.


  • 0
    L

    Thanks for your help and my answer has been accepted.


Log in to reply
 

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