Given an input string, reverse the string word by word.
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
public String reverseWords(String s) {
if(s == null || s.length() == 0){
return "";
}
String[] arr = s.split(" ");
StringBuffer sb = new StringBuffer();
for(int i = arr.length - 1; i >= 0; --i){
if(!arr[i].equals("")){
sb.append(arr[i]).append(" ");
}
}
return sb.length() == 0 ? "" : sb.substring(0, sb.length() - 1);
}
参考:{LeetCode – Reverse Words in a String (Java)
}{http://www.programcreek.com/2014/02/leetcode-reverse-words-in-a-string-java/}