https://leetcode.com/problems/reverse-words-in-a-string/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// class Solution {
// public String reverseWords(String s) {
// String[] sarr=s.trim().split(" ");
// int len=sarr.length;
// String res="";
// for(int i=len-1;i>=0;i--){
// if(sarr[i].length()!=0) res+=sarr[i]+" ";
// }
// return res.trim();
// }
// }
class Solution {
public String reverseWords(String s) {
List<String> wordList=Arrays.asList(s.trim().split("\\s+"));
Collections.reverse(wordList);
return String.join(" ",wordList);
}
}

0%