cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

(String)151. Reverse Words in a String

Posted on 2016-04-10 02:48  cKK  阅读(142)  评论(0编辑  收藏  举报

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) {
    String afterTrim= s.trim();
    String[] split=afterTrim.split(" +");  //注意res
    StringBuilder sb= new StringBuilder();
    for(int i=split.length-1;i>=0;i--){
         sb.append(split[i]+" "); 
    }
    return sb.toString().trim();
    }
}