-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathMinStack.java
More file actions
38 lines (30 loc) · 755 Bytes
/
MinStack.java
File metadata and controls
38 lines (30 loc) · 755 Bytes
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
package datastructure.stack.leetcode;
import java.util.Stack;
/**
* @author roseduan
* 最小栈
*/
public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> helper;
/** initialize your data structure here. */
public MinStack() {
this.stack = new Stack<>();
this.helper = new Stack<>();
this.helper.push(Integer.MAX_VALUE);
}
public void push(int x) {
this.stack.push(x);
this.helper.push(Math.min(x, this.helper.peek()));
}
public void pop() {
this.stack.pop();
this.helper.pop();
}
public int top() {
return this.stack.peek();
}
public int getMin() {
return this.helper.peek();
}
}