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.
Subscribe to see which companies asked this question
没难度吧……split一下就好
public class Solution {
public String reverseWords(String s) {
String ret="";
String [] list = s.split(" ");
for(int i = list.length-1;i>=0;i--){
if(list[i].length()==0)continue;
ret+=' '+list[i];
}
if(ret.length()==0)return ret;
ret=ret.substring(1,ret.length());
return ret;
}
}