[leetcode]Min Stack

就是用另外一个单调stack来记录最小值就可以了,这个队列单调递减。

class MinStack {
public:
    void push(int x) {
        st.push(x);
        if (stm.empty() || stm.top() >= x) stm.push(x);
    }

    void pop() {
        int top = st.top(); st.pop();
        if (top <= stm.top()) stm.pop();
    }

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

    int getMin() {
        return stm.top();
    }
private:
    stack<int> st;
    stack<int> stm;
};
原文地址:https://www.cnblogs.com/x1957/p/4086448.html