1.题目链接
力扣
2.解题思路
利用两个栈实现
3.代码
class MinStack {
public:
    MinStack() {
        //两个栈来实现
    }
    void push(int val) {
        _st.push(val);
        if (_minst.empty() || _minst.top() >= val)
        {
            _minst.push(val);
        }
    }
    void pop() {
        if (_st.top() == _minst.top())
            _minst.pop();
        _st.pop();
    }
    int top() {
        return _st.top();
    }
    int getMin() {
        return _minst.top();
    }
    stack<int> _st;
    stack<int> _minst;
};
4.运行结果

【C++ OJ练习】9.最小栈 完



















