包含min函数的栈

class MinStack {
public:
    stack<int> st;//普通栈
    stack<int> stMin;//单调栈
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        st.push(x);
        if(stMin.empty()||stMin.top()>=x)
            stMin.push(x);
    }
    
    void pop() {
        if(st.top()==stMin.top())   stMin.pop();
        st.pop();
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return stMin.top();
    }
};
posted @ 2023-03-29 15:41  穿过雾的阴霾  阅读(15)  评论(0)    收藏  举报