forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT4.java
More file actions
37 lines (29 loc) · 746 Bytes
/
T4.java
File metadata and controls
37 lines (29 loc) · 746 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
/**
* @program JavaBooks
* @description: 最小栈(两个栈实现)
* @author: mf
* @create: 2020/03/12 11:53
*/
package subject.sq;
import java.util.Stack;
public class T4 {
private Stack<Integer> dataStack = new Stack<>();
private Stack<Integer> minStack = new Stack<>();
private Integer min = Integer.MAX_VALUE;
public void push(int x) {
dataStack.push(x);
min = Math.min(x, min);
minStack.push(min);
}
public void pop() {
dataStack.pop();
minStack.pop();
min = minStack.isEmpty() ? Integer.MAX_VALUE : minStack.peek();
}
public int top() {
return dataStack.peek();
}
public int getMin() {
return minStack.peek();
}
}