AcWing 41. 包含min函数的栈
原题链接
简单
作者:
沙漠绿洲
,
2020-08-19 23:29:39
,
所有人可见
,
阅读 402
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
s1.push(x);
if(s2.empty() || s2.top() > x) s2.push(x);
}
void pop() {
if(s2.size() && s1.top() == s2.top()) s2.pop();
s1.pop();
}
int top() {
return s1.top();
}
int getMin() {
return s2.top();
}
stack<int> s1, s2;
};
/**
* 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();
*/