剑指offer速通
小于 1 分钟
剑指offer速通
栈
1.剑指 Offer 30. 包含min函数的栈 - 力扣(LeetCode) (leetcode-cn.com)
class MinStack {
stack<int >sm ,s;
public:
/** initialize your data structure here. */
MinStack() {
while(!sm.empty()){
sm.pop();
}
while(!s.empty()){
s.pop();
}
}
void push(int x) {
s.push(x);
if(sm.empty()||x<=sm.top()){
sm.push(x);
}
}
void pop() {
if(sm.top()==s.top()){
sm.pop();
}
s.pop();
}
int top() {
return s.top();
}
int min() {
return sm.top();
}
};
/**
* 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();
*/