• 剑指 Offer 30. 包含min函数的栈


    单调栈。

    class MinStack {
    public:
        stack<int> stk, minstk;
        /** initialize your data structure here. */
        MinStack() {
        
        }
        
        void push(int x) {
            stk.push(x);
            if (minstk.empty() || x <= minstk.top())
                minstk.push(x);
        }
        
        void pop() {
            int x = stk.top();
            stk.pop();
            if (x == minstk.top())
                minstk.pop();
        }
        
        int top() {
            return stk.top();
        }
        
        int min() {
            return minstk.top();
        }
    };
    
    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack* obj = new MinStack();
     * obj->push(x);
     * obj->pop();
     * int param_3 = obj->top();
     * int param_4 = obj->min();
     */
    
  • 相关阅读:
    libusbwin32
    KMP
    windows.h
    iomanip
    C++继承
    LIST
    fstream
    VS2010中调试c++程序的方法
    sstream
    char 与char* 字符串与字符
  • 原文地址:https://www.cnblogs.com/fxh0707/p/15049092.html
Copyright © 2020-2023  润新知