Skip to content

Commit 8022168

Browse files
committed
fix: change Deque to Stack for readability and familiarity of using Stack
1 parent 9c25456 commit 8022168

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

java/Stacks/EvaluateExpression.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import java.util.ArrayDeque;
2-
import java.util.Deque;
1+
import java.util.Stack;
32

43
public class EvaluateExpression {
54
public int evaluateExpression(String s) {
6-
Deque<Integer> stack = new ArrayDeque<>();
5+
Stack<Integer> stack = new Stack<>();
76
int currNum = 0;
87
int sign = 1;
98
int res = 0;

java/Stacks/MaximumsOfSlidingWindow.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import java.util.ArrayDeque;
2-
import java.util.Deque;
1+
import java.util.Stack;
32

43
public class MaximumsOfSlidingWindow {
54
public int[] maximumsOfSlidingWindow(int[] nums, int k) {
65
int[] res = new int[nums.length - k + 1];
7-
Deque<int[]> dq = new ArrayDeque<>();
6+
Stack<int[]> dq = new Stack<>();
87
int left, right;
98
left = right = 0;
109
while (right < nums.length) {
1110
// 1) Ensure the values of the deque maintain a monotonic decreasing order
1211
// by removing candidates <= the current candidate.
13-
while (!dq.isEmpty() && dq.peekLast()[0] <= nums[right]) {
14-
dq.pollLast();
12+
while (!dq.isEmpty() && dq.peek()[0] <= nums[right]) {
13+
dq.pop();
1514
}
1615
// 2) Add the current candidate.
17-
dq.offerLast(new int[]{nums[right], right});
16+
dq.push(new int[]{nums[right], right});
1817
// If the window is of length 'k', record the maximum of the window.
1918
if (right - left + 1 == k) {
2019
// 3) Remove values whose indexes occur outside the window.
21-
if (!dq.isEmpty() && dq.peekFirst()[1] < left) {
22-
dq.pollFirst();
20+
if (!dq.isEmpty() && dq.firstElement()[1] < left) {
21+
dq.remove(0);
2322
}
2423
// The maximum value of this window is the leftmost value in the
2524
// deque.
26-
res[left] = dq.peekFirst()[0];
25+
res[left] = dq.firstElement()[0];
2726
// Slide the window by advancing both 'left' and 'right'. The right
2827
// pointer always gets advanced so we just need to advance 'left'.
2928
left++;

java/Stacks/RepeatedRemovalOfAdjacentDuplicates.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import java.util.ArrayDeque;
2-
import java.util.Deque;
1+
import java.util.Stack;
32

43
public class RepeatedRemovalOfAdjacentDuplicates {
54
public String repeatedRemovalOfAdjacentDuplicates(String s) {
6-
Deque<Character> stack = new ArrayDeque<>();
5+
Stack<Character> stack = new Stack<>();
76
StringBuilder res = new StringBuilder();
87
for (char c : s.toCharArray()) {
98
// If the current character is the same as the top character on the stack,
@@ -19,7 +18,7 @@ public String repeatedRemovalOfAdjacentDuplicates(String s) {
1918
}
2019
// Return the remaining characters as a string.
2120
for (char c : stack) {
22-
res.insert(0, c);
21+
res.append(c);
2322
}
2423
return res.toString();
2524
}

0 commit comments

Comments
 (0)