题解 | #翻转单词序列#
翻转单词序列
http://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3
遍历的时候将字符串从前到后加入到StringBuilder中,然后注意空格输出就行。
public String ReverseSentence(String str) {
if (str == null ||str.length()==0) {
return "";
}
String[] strings = str.split(" ");
StringBuilder sb = new StringBuilder();
int len = strings.length;
for (int i = 0; i < len; i++) {
if (i == len-1) {
sb.append(strings[len-1-i]);
}else {
sb.append(strings[len-1-i]+" ");
}
}
return sb.toString();
}
}