zoukankan      html  css  js  c++  java
  • leetcode155 min stack

    Description

    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.

    Problem

    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->getMin();

    易错点

    注意考虑记录指针index永远指向当前元素/下一元素

    Code

    class MinStack {
    public:
        /** initialize your data structure here. */
        MinStack() {
        }
        
        void push(int x) {
            if(s.empty())mini[++index]=x;
            else mini[++index]=min(mini[index],x);
            s.push(x);
        }
        
        void pop() {
            index--;
            s.pop();
        }
        
        int top() {
            return s.top();
        }
        
        int getMin() {
            return mini[index];
        }
    private:
        stack<int> s;
        int mini[50000];
        int index=-1;
    };
    
  • 相关阅读:
    单分发器
    Python 虚拟环境
    $modal
    memoization
    directive例子2
    UI-Router
    angular-translate
    directive例子1
    FileInputStream/FileOutputStream的应用
    自定义readLine
  • 原文地址:https://www.cnblogs.com/chanceYu/p/12879700.html
Copyright © 2011-2022 走看看