解决思路
public class Solution {
public String reverseWords(String s) {
String words[] = s.split(" ");
StringBuilder res=new StringBuilder();
for (String word: words)
res.append(new StringBuffer(word).reverse().toString() + " ");
return res.toString().trim();
}
}
public class Solution {
public String reverseWords(String input) {
final StringBuilder result = new StringBuilder();
final StringBuilder word = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ') {
word.append(input.charAt(i));
} else {
result.append(word.reverse());
result.append(" ");
word.setLength(0);
}
}
result.append(word.reverse());
return result.toString();
}
}