剑指 Offer 30. 包含min函数的栈 - 力扣(LeetCode) (leetcode-cn.com)
class MinStack {
public:
/** initialize your data structure here. */
stack<int>s;
int Min=INT32_MAX;
MinStack() {
}
void push(int x) {
s.push(Min);
if(x<Min) Min=x;
s.push(x);
}
void pop() {
s.pop();
Min=s.top();
s.pop();
}
int top() {
return s.top();
}
int min() {
return Min;
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->min();
*/