zoukankan      html  css  js  c++  java
  • LeetCode:Min Stack(待更新)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • getMin() -- Retrieve the minimum element in the stack.
    class MinStack {
    private:  
        std::stack<int> stack;  
        std::stack<int> min_stack;  
    public:  
        void push(int x) {  
            stack.push(x);  
            if (min_stack.empty() || ((!min_stack.empty()) && x <= min_stack.top())) {  
                min_stack.push(x);  
            }  
        }  
          
        void pop() {  
            if (!stack.empty()) {  
                if (stack.top() == min_stack.top())  
                    min_stack.pop();  
                stack.pop();  
            }  
        }  
          
        int top() {  
            if (!stack.empty())  
                return stack.top();  
        }  
          
        int getMin() {  
            if (!min_stack.empty())  
                return min_stack.top();  
        }  
    };
  • 相关阅读:
    进制转换
    BZOJ 1232 安慰奶牛题解
    [BeiJing wc2012]冻结 题解
    the Captain题解;
    最佳贸易
    第二短路
    街道赛跑
    图论基础知识.
    Tkinter 之事件绑定
    Tkinter 之TopLevel顶级窗口
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4702143.html
Copyright © 2011-2022 走看看