151. Reverse Words in a String
str.trim.split(" +") 然后用StringBuilder
https://leetcode.com/problems/reverse-words-in-a-string/discuss/161008/6-line-Java-answer
1 //Old 2 class Solution { 3 public String reverseWords(String s) { 4 if(s.length() == 0) return s; 5 char[] arr = s.toCharArray(); 6 List<String> list = new ArrayList<>(); 7 for(int i = 0; i < arr.length; i++) { 8 String str = ""; 9 while( i < arr.length && arr[i] != ' ') { 10 str += arr[i]; 11 i++; 12 } 13 if(str.length() != 0) { 14 list.add(new String(str)); 15 } 16 } 17 String res = ""; 18 for(int i = list.size() - 1; i >= 0; i--) { 19 res += list.get(i); 20 if(i != 0) res += " "; 21 } 22 return res; 23 24 } 25 }