最小栈

思想分析:

实现方案:

class MinStack
{
public:
    MinStack()
    {}

    void push(int x)
    {
        if (StackNum.empty())
        {
            StackNum.push(x);
            StackMin.push(x);
        }
        else
        {
            if (x <= StackMin.top())
                StackMin.push(x);
            StackNum.push(x);
        }
    }

    void pop()
    {
        if (StackMin.top() == StackNum.top())
            StackMin.pop();
        StackNum.pop();
    }

    int top()
    {
        return StackNum.top();
    }

    int getMin()
    {
        return StackMin.top();
    }
protected:
    stack<int> StackNum;
    stack<int> StackMin;
};
两个栈实现最小栈
原文地址:https://www.cnblogs.com/shihaochangeworld/p/5665086.html