forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_151.java
30 lines (28 loc) · 935 Bytes
/
_151.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
public class _151 {
public static class Solution1 {
public String reverseWords(String s) {
s.trim();
if (s == null || s.length() == 0) {
return "";
}
String[] words = s.split(" ");
if (words == null || words.length == 0) {
return "";
}
Deque<String> stack = new ArrayDeque<>();
for (String word : words) {
if (!word.equals("")) {
stack.offer(word);
}
}
StringBuilder stringBuilder = new StringBuilder();
while (!stack.isEmpty()) {
stringBuilder.append(stack.pollLast()).append(" ");
}
return stringBuilder.substring(0, stringBuilder.length() - 1).toString();
}
}
}