zoukankan      html  css  js  c++  java
  • leetcode 155 最小栈

    简介

    使用系统库

    code

    class MinStack {
    public:
        /** initialize your data structure here. */
        deque<int> s;
        MinStack() {
    
        }
        
        void push(int val) {
            s.push_back(val);
        }
        
        void pop() {
            s.pop_back();
        }
        
        int top() {
            return s.back();
        }
        
        int getMin() {
            int minn = s[0];
            for(auto it: s){
                minn = min(it, minn);
            }
            return minn;
        }
    };
    
    class MinStack {
        Deque<Integer> s;
        /** initialize your data structure here. */
        public MinStack() {
            s = new LinkedList<Integer>();
        }
        
        public void push(int val) {
            s.push(val);
        }
        
        public void pop() {
            s.pop();
        }
        
        public int top() {
            return s.peek(); // 顶部竟然返回的是peek, 还是C++直观
        }
        
        public int getMin() {
            int imin = Integer.MAX_VALUE;
            for(Integer it:s){
                imin = Math.min(it, imin);
            }
            return imin;
        }
    }
    
    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack obj = new MinStack();
     * obj.push(val);
     * obj.pop();
     * int param_3 = obj.top();
     * int param_4 = obj.getMin();
     */
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    07周总结
    06周总结
    05周总结
    04周总结
    03周总结
    02周总结
    python数据特征预处理
    LeetCode Hard: 23. Merge k Sorted Lists
    LeetCode Hard: 4. Median of Two Sorted Arrays
    LeetCode Medium: 49. Group Anagrams
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14778384.html
Copyright © 2011-2022 走看看