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();  
        }  
    };
  • 相关阅读:
    Servlet的生命周期及工作原理
    抓包---firebug
    firebug抓包
    token认证来龙去脉
    性能测试报告注意事项
    性能测试报告
    Error -26601解决办法
    lr新手误区
    css定位
    xpath定位
  • 原文地址:https://www.cnblogs.com/xiaoying1245970347/p/4702143.html
Copyright © 2011-2022 走看看