1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/java/reverse-words-in-a-string.java

17 lines
349 B
Java
Raw Normal View History

class Solution {
private static <T> void reverse(T[] arr) {
for (int l = 0, r = arr.length - 1; l < r; ++l, --r) {
var tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
}
}
public String reverseWords(String s) {
String[] words = s.strip().split("\\s+");
reverse(words);
return String.join(" ", words);
}
}