forked from MisterBooo/LeetCodeAnimation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.java
More file actions
37 lines (31 loc) · 689 Bytes
/
1.java
File metadata and controls
37 lines (31 loc) · 689 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
class MinStack {
private Stack<Integer> _data;
private Stack<Integer> _min;
/** initialize your data structure here. */
public MinStack() {
_data = new Stack<>();
_min = new Stack<>();
}
public void push(int x) {
_data.add(x);
if (_min.isEmpty()){
_min.push(x);
}
else{
if (x > _min.peek()){
x = _min.peek();
}
_min.push(x);
}
}
public void pop() {
_data.pop();
_min.pop();
}
public int top() {
return _data.peek();
}
public int getMin() {
return _min.peek();
}
}