Total Accepted: 154413
Total Submissions: 982629
Difficulty: Medium
Contributor: LeetCode
Given an input string, reverse the string word by word.
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show clarification.
Subscribe to see which companies asked this question.
自己写出来以后看了别人的代码,精简的多。主要是自己对于JAVA自带的方法还不是特别的熟悉,AC码如下,要注意在split()方法中,‘+’号是正则表达式里的意思,如果真正要使用+号需要转义:
public String reverseWords(String s) {
String[] substr = s.trim().split(" +");
Collections.reverse(Arrays.asList(substr));
return String.join(" ",substr);
}