-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack03_02.java
More file actions
48 lines (42 loc) · 1 KB
/
Stack03_02.java
File metadata and controls
48 lines (42 loc) · 1 KB
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
44
45
46
47
48
package stack;
import java.util.Stack;
/**
* @ProjectName: leetcode
* @Package: stack
* @ClassName: Stack03_02
* @Author: markey
* @Description:
* @Date: 2020/6/5 23:30
* @Version: 1.0
*/
public class Stack03_02 {
class MinStack {
Stack<Integer> stack;
Stack<Integer> minStack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
stack.push(x);
if (!minStack.isEmpty() && minStack.peek() < x) {
minStack.push(minStack.peek());
} else {
minStack.push(x);
}
}
public void pop() {
if (!stack.isEmpty()) {
stack.pop();
minStack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
}