Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
public class Solution {
public String reverseWords(String s) {
//"\\s"表示空格,回车,换行等空白符,"+"表示一个或多个
String[] parts = s.trim().split("\\s+");
String out = "";
if (parts.length > 0) {
for (int i = parts.length - 1; i > 0; i--) {
out += parts[i] + " ";
}
out += parts[0];
}
return out;
}
}