Topic:
Given an input string, reverse the string word by word.
For example,
Given s = "The Sky is Blue",
Return "Blue is Sky the".
Idea One:
Take a break for the given string, remove more than one of the spaces, is to make the space between all the words into one, and then remove the most behind the space, to the front plus a space. So each time a space is encountered, the front substring is a complete word.
C + + code implementation (spents 9MS):
classsolution{ Public:voidReversewords (string&s) {//Remove all more than one space for(size_t i =1; I < s.length (); i++) {if(S[i-1] =="'&& S[i] = ="') {S.erase (S.begin () + i); I.; } }//If there are no spaces ahead, add a space to the front if(S.front ()! ="') S.insert (0," ");//If there is a space behind, remove the trailing space if(S.back () = ="') S.pop_back ();if(S.length () <=1)return; vector<string>WordsintStart =int(S.length ()-1);//traverse from backward to forward for(inti = start; I >=0; -I.) {if(S[i] = ="') {Words.push_back (S.substr (i +1, start-i));if(I! =0) Start = i-1; }} s.clear (); size_t size = Words.size (); for(size_t i =0; i < size; i++) {s + = words[i]; S.push_back ("'); }//Remove the last extra spaceS.pop_back (); }};
Idea two:
Maintain two stacks, when it is not a space to press into the word stack, when the space is pressed into the sentence stack. The advantage of this is that you don't have to deal with a given string beforehand.
C + + Implementation code (time 24ms, probably the stack overhead is relatively large bar):
classsolution{ Public:voidReversewords (string&s) {s + ="';//Because when S[i] is a space, the preceding string is a word, and for good judgment, add a space after the charactersize_t length = S.length (); Stack<char>Words Stack<char>sentence; for(size_t i =0; i < length; i++) {//If not a space, press into the words stack if(S[i]! ="') Words.push (S[i]);//If it is a space and words is not empty, press the characters in words into the sentence stack and add vacancies after sentence //If it is a space but words is empty then do nothing Else{BOOLFlag =false; while(!words.empty ()) {flag =true; Sentence.push (Words.top ()); Words.pop (); }if(flag) Sentence.push ("'); }} s.clear ();//If the sentence is not empty, there must be an extra space behind, removing the last space if(!sentence.empty ()) Sentence.pop (); while(!sentence.empty ()) {s + = Sentence.top (); Sentence.pop (); } }};
C # code (Thinking One solution):
Public classsolution{ Public string Reversewords(strings) {if(S.length = =0)return string. Empty; StringBuilder result =NewStringBuilder (s);//Remove more than one space for(inti =1; I < result. Length; i++) {if(Result[i-1] =="'&& Result[i] = ="') {result. Remove (I,1); I.; } }if(result[0] !="') result. Insert (0,"');if(Result[result. Length-1] =="') result. Remove (result. Length-1,1);if(result.) Length <=1)returnResult. ToString (); s = result. ToString (); list<string> words =Newlist<string> ();intStart = result. Length-1; for(inti = start; I >=0; -I.) {if(Result[i] = ="') {words. ADD (s.substring (i +1, start-i)); start = i-1; }} result. Clear ();foreach(stringIteminchwords) {result. Append (item); Result. Append ("'); } result. Remove (result. Length-1,1);returnResult. ToString (); }}
Python code:
Class Solution:# @param s, a string # @return A stringdef reversewords (self, s):if Len(s) = =0:return "' words= S.Split("') i =0 whileI <Len(words):if words[I] = ="': DelwordsIElse: i = i +1 if Len(words) ==0:return "' words. Reverse ()result="' for Item inch words:result=result+Item+"' return result[:Len(result) -1]
Obviously, Python code is much more concise!
Leetcode:reverse Words in a String