Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
题目含义:翻转字符串中的单词的顺序,单词内容不变
1 public String reverseWords(String s) { 2 s = s.trim(); 3 String[]words = s.split("\s+");//正则表达式s表示匹配任何空白字符,+表示匹配一次或多次。 4 if (words.length <2) return s; 5 int low=0,high=words.length-1; 6 while (low<high) 7 { 8 String temp = words[low]; 9 words[low] = words[high]; 10 words[high] = temp; 11 low++; 12 high--; 13 } 14 StringBuilder sb = new StringBuilder(); 15 for (String word:words) 16 { 17 sb.append(word).append(" "); 18 } 19 return sb.toString().trim(); 20 } 21 }