class Solution {
public String reverseWords(String s) {
s = s.trim();
int i = s.length() - 1;
int j = i;
StringBuffer sb = new StringBuffer();
while (i >= 0) {
while (i >= 0 && s.charAt(i) != ' ') {
i--;
}
sb.append(s.substring(i + 1, j + 1) + " ");
while (i >= 0 && s.charAt(i) == ' ') {
i--;
}
j = i;
}
return sb.toString().trim();
}
}
class Solution {
public String reverseWords(String s) {
s = s.trim();
char[] str = s.toCharArray();
int end = str.length - 1;
int start = end;
StringBuffer res = new StringBuffer();
while (start >= 0) {
while (start >= 0 && str[start] != ' ') {
start--;
}
for (int index = start + 1; index <= end; index++) {
res.append(str[index]);
}
res.append(' ');
end = start;
while (start >= 0 && str[start] == ' ') {
start--;
end--;
}
}
return new String(res).trim();
}
}