-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMinStack.java
43 lines (34 loc) · 852 Bytes
/
MinStack.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
31
32
33
34
35
36
37
38
39
40
41
42
43
package io.ziheng.stack.leetcode;
import java.util.Deque;
import java.util.LinkedList;
/**
* LeetCode 155. Min Stack
* https://leetcode.com/problems/min-stack/
*/
public class MinStack {
private Deque<Integer> normalStack;
private Deque<Integer> minStack;
public MinStack() {
this.normalStack = new LinkedList<>();
this.minStack = new LinkedList<>();
}
public void push(int x) {
normalStack.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) {
minStack.push(x);
}
}
public void pop() {
if (normalStack.peek().equals(minStack.peek())) {
minStack.pop();
}
normalStack.pop();
}
public int top() {
return normalStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
/* EOF */