leetcode-最小栈
用到一个辅助栈来存储最小值,注意点的就是两个栈是否同时弹出值
class MinStack
{
public:
MinStack() {
}
stack<int> s;
stack<int> Min;
void push(int x) {
if(Min.empty()||x<=Min.top())
{
Min.push(x);
}
s.push(x);
}
void pop() {
if(!s.empty())
{
if(Min.top()==s.top())
{
Min.pop();
s.pop();
}
else
s.pop();
}
}
int top() {
return s.top();
}
int getMin() {
return Min.top();
}
};
原文:https://www.cnblogs.com/tonghang123/p/12851436.html