AcWing 41. 包含min函数的栈
原题链接
简单
作者:
Value
,
2020-09-01 19:56:32
,
所有人可见
,
阅读 351
class MinStack {
public:
/** initialize your data structure here. */
stack<int> stk, minv;
MinStack() {
}
void push(int x) {
stk.push(x);
if(minv.empty()) minv.push(x);
else{
int tmp = minv.top();
minv.push(min(tmp, x));
}
}
void pop() {
stk.pop();
minv.pop();
}
int top() {
return stk.top();
}
int getMin() {
return minv.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.getMin();
*/