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
原文地址:https://www.cnblogs.com/eat-too-much/p/14778384.html