mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-31 15:25:33 +08:00
37 lines
689 B
Java
37 lines
689 B
Java
|
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();
|
||
|
}
|
||
|
}
|