题目链接:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/
题目如下:
class MinStack {
stack<int> stk;
int min_number=INT_MAX;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {//让最底的元素为INT_MAX,让每次push2个数值进去,一个为最小,一个为当前值
stk.push(min_number);
if(x<min_number) min_number=x;
stk.push(x);
}
void pop() {//由于每次push进去的是2个数值,故此,为此pop应pop两个数值
stk.pop();
min_number=stk.top();
stk.pop();
}
int top() {
return stk.top();
}
int min() {
return min_number;
}
};
/**
* 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();
*/