原题链接:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
图示:
class MinStack {
public:
stack<int> A, B;//A为正常栈,B为单调栈
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
A.push(x);
if (B.empty() || B.top() >= x)
B.push(x);
}
void pop() {
if (A.top() == B.top())
B.pop();
A.pop();
}
int top() {
return A.top();
}
int min() {
return B.top();
}
};