思路: reverse the single word first from the rear to front. Then, append the reverse word to the StringBuilder.
public class Solution {
public String reverseWords(String s) {
s = s.trim();
int index = s.length();
if(index < 2)
return s;
StringBuilder sb = new StringBuilder();
index--;
while(index >= 0){
StringBuilder curWord = new StringBuilder();
while(index >= 0 && s.charAt(index) != ' '){
curWord.append(s.charAt(index));
index--;
}
sb.append(curWord.reverse()).append(' ');
while(index >= 0 && s.charAt(index) == ' '){
index--;
}
}
return sb.toString().trim();
}
}