[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;
};

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM