Running correctly locally , got a Runtime Error ?


  • 0
    V

    Here is my C++ code , I run it locally correctly , but got a Runtime Error and a message ' last executed input "" '

    Can anyone be kind to read my poor code and find the bug?

    Thx :)

    class Solution {
    	public:
    		void reverseWords(string &s) {			
    			string revStr("");
    			int start, end;			
    			end = start = s.length();
    			s+=' ';			
    			bool should_stop = false;
    			//here we go
    			if(s!= " ")
    			{
    				while(start >= 0)
    				{
    					//escape space
    					while(s[start] == ' ') 
    					{
    						start--;
    						if(start<0)
    						{
    							should_stop = true;
    							break;
    						}
    					}
    					if(should_stop == true)
    					{
    						break;
    					}
    					end = start;
    					//escape word
    					while(s[start] != ' ')
    					{
    						start--;
    						if(start<0)
    						{						
    							break;
    						}
    					}
    					//copy word
    					revStr += s.substr(start+1,end-start);
    					revStr += ' ';
    				}
    				revStr = revStr.substr(0,revStr.length()-1);
    			}
    			s=revStr;
    		}
    };

  • 0
    H

    I guess the reason might be s[start] is out of index.


  • 0
    F

    I run it correctly online in leetcode. You can try it again.


Log in to reply
 

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