Given an input string, reverse the string word by word.
For example,
Given s = " the sky is blue ",
Return " blue is sky the ".
Update (2015-02-12):
For C programmers:try to solve it in-place in O(1) space.
Reversed string.
Note Using bufferstring, because if you use a string, the object will be created more than once, which is slow.
Public classSolution { Publicstring Reversewords (string s) {intLen =s.length (); if(len = = 0) returns; StringBuffer result=NewStringBuffer (); intend = Len-1,start = 0; while(End >= 0 && s.charat (end) = = ") End--; if(end = =-1) returnresult.tostring (); while(Start < Len && S.charat (start) = = ") Start++; intpos = end+1; for(inti = end; I >= start; i-- ){ if(S.charat (i) = = ") {result.append (s.substring (i+1, POS)); Result.append (" "); while(I < end && S.charat (i) = = ") I--; I++; POS=i; }} result.append (S.substring (Start,pos)); returnresult.tostring (); }}
Leetcode 151. Reverse Words in a String---------Java