方法1:辅助栈
时间复杂度:$O(1)$
空间复杂度:$O(n)$
解题思路
https://leetcode.cn/problems/min-stack/solution/min-stack-fu-zhu-stackfa-by-jin407891080/
Java 代码
class MinStack {
Stack<Integer> stk1;
Stack<Integer> stk2;
public MinStack() {
stk1 = new Stack<>();
stk2 = new Stack<>();
}
public void push(int val) {
stk1.push(val);
if (stk2.isEmpty() || val <= stk2.peek()) {
stk2.push(val);
}
}
public void pop() {
if (stk1.pop().equals(stk2.peek())) {
stk2.pop();
}
}
public int top() {
return stk1.peek();
}
public int getMin() {
return stk2.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/