class MinStack {
public:
MinStack()
{
coll.resize(2);
}
void push(int x) {
if(index == coll.size()-1)
coll.resize(2*coll.size());
coll[++index]=x;
if(x<min)
{
min=x;
flag=index;
}
}
void pop() {
index--;
}
int top() {
return coll[index];
}
int getMin() {
if(index <flag&& index!=-1)
{
min=coll[0];
for(int i=1;i<=index;i++)
if(coll[i]<min)
{
min=coll[i];
flag=i;
}
}
return min;
}
private:
vector<int> coll;
int index=-1;
int min=INT_MAX,flag;
};
转载于:https://www.cnblogs.com/yanqi110/p/5013141.html