AcWing 41. 包含min函数的栈
原题链接
简单
作者:
你的小猫咪呀0v0
,
2019-12-16 22:17:50
,
所有人可见
,
阅读 743
class MinStack {
public:
/** initialize your data structure here. */
int a[10001];
int b[10001];
int at=-1;
int bt=-1;
MinStack() {
}
void push(int x) {
a[++at]=x;
if(bt!=-1)
{
if(x<=b[bt])
{
b[++bt]=x;
}
}
else
{
b[++bt]=x;
}
}
void pop() {
at--;
if(a[at+1]==b[bt])
{
bt--;
}
}
int top() {
return a[at];
}
int getMin() {
return b[bt];
}
};
/**
* 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();
*/