Java:1
2
3
4
5public String reverseWords(String s) {
String[] words = s.trim().split(" +");
Collections.reverse(Arrays.asList(words));
return String.join(" ", words);
}
list才是Collections的一种,才能用reverse;String.join();
Python: easier:1
return " ".join(s.strip().split()[::-1])