标签:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
思路:采用String 的 split 方法进行分割 , 其分割的正则表达式为" ".
1 public class Solution { 2 /** 3 * @param s : A string 4 * @return : A string 5 */ 6 public String reverseWords(String s) { 7 if(s == null || s.length() == 0) { 8 return ""; 9 } 10 String[] words = s.split(" "); 11 StringBuilder builder = new StringBuilder(); 12 for (int i = words.length - 1; i >= 0; i--) { 13 if (words[i] != " ") { 14 builder.append(words[i]).append(" "); 15 } 16 } 17 return builder.length() == 0 ? "":builder.substring(0, builder.length() - 1); 18 } 19 }
标签:
原文地址:http://www.cnblogs.com/FLAGyuri/p/5554577.html