class MinStack {
private:
long min;
stack<long> *lstack;
public:
/** initialize your data structure here. */
MinStack() {
lstack=new stack<long>();
}
void push(int x) {
if(lstack->empty())
{
lstack->push(0L);
min=long(x);
}
else
{
lstack->push(long(x-min));
if(x<min)
min=x;
}
}
void pop() {
long temp=lstack->top();
lstack->pop();
if(temp<0)
min-=temp;
}
int top() {
long temp=lstack->top();
if(temp>=0)
return int(min+temp);
else
return int(min);
}
int getMin() {
return int(min);
}
};
155. Min Stack
最新推荐文章于 2020-08-03 10:03:11 发布