[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