forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT5.java
More file actions
38 lines (30 loc) · 626 Bytes
/
T5.java
File metadata and controls
38 lines (30 loc) · 626 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
/**
* @program JavaBooks
* @description: 最小栈
* @author: mf
* @create: 2020/03/12 14:44
*/
package subject.sq;
import java.util.Stack;
public class T5 {
private Integer min = Integer.MAX_VALUE;
private Stack<Integer> stack = new Stack<>();
public void push(int x) {
if (x <= min) {
stack.push(min);
min = x;
}
stack.push(x);
}
public void pop() {
if (stack.pop() == min) {
min = stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}