本题 题目链接
题目描述
我的题解
方法:【转自leetcode大佬】
思路分析
代码如下
Stack<Integer> a,b; // a是主栈,b是辅助栈
/** initialize your data structure here. */
public MinStack() {
a = new Stack<>();
b = new Stack<>();
}
public void push(int x) {
a.add(x);
if (b.isEmpty() || x <= b.peek()) b.add(x);
}
public void pop() {
if (!b.isEmpty() && b.peek().equals(a.peek()) ) b.pop();
a.pop();
}
public int top() {
return a.peek();
}
public int min() {
return b.peek();
}