Reverse Words in a String
Given an input string, reverse the string word by word.
For example, Given s = "the sky is blue", return "blue is sky the".
思路: 把string以空格为间隔分隔开存入array, 然后倒着加入stringBuilder并且每个加入以后后面加空格,最后记的清除最后一个空格。
public class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return "";
}
String[] array = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = array.length - 1; i >= 0; i--) {
if (!array[i].equals("")) {
sb.append(array[i]).append(" ");
}
}
//remove the last " "
return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
}
}