forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (31 loc) · 805 Bytes
/
Solution.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
class MinStack {
private Stack<Integer> stack;// = new Stack<>();
private Stack<Integer> minStack;// = new Stack<>();
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack<>();
this.minStack = new Stack<>();
}
public void push(int x) {
this.stack.push(x);
if (this.minStack.isEmpty()) {
this.minStack.push(x);
} else {
if (x < this.minStack.peek()) {
this.minStack.push(x);
} else {
this.minStack.push(this.minStack.peek());
}
}
}
public void pop() {
this.stack.pop();
this.minStack.pop();
}
public int top() {
return this.stack.peek();
}
public int getMin() {
return this.minStack.peek();
}
}