zoukankan      html  css  js  c++  java
  • 155. 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.

    Example:

    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin();   --> Returns -3.
    minStack.pop();
    minStack.top();      --> Returns 0.
    minStack.getMin();   --> Returns -2.
    

    存一个instance variable表示到目前为止的最小值
    stack里存的是:
         如果当前值大于之前的最小值,存当前值-最小值(正数)
         如果当前值小于之前的最小值,则这个值变成新的最小值,存当前值-最小值(负数)并更新最小值
    pop: pop出栈顶元素cur, 如果cur < 0,说明当前pop出的值是当前最小值,出栈后把最小值更新为min - cur; 如果cur > 0, 说明当前值大于当前最小值,应该是cur + pop
    top: 栈顶元素cur = stack.peek(), 如果cur < 0, 说明当前值是最小值,返回min,如果cur> 0, 说明当前值大于最小值,实际为cur + min
    public class MinStack {
        Stack<Long> stack;
        int min;
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<Long>();
            min = 0;
        }
        
        public void push(int x) {
            if (stack.isEmpty()) {
                min = x;
                stack.push(0L);
                return;
            }
            stack.push((long)x - min);
            if (x < min) {
                min = x;
            }
        }
        
        public void pop() {
            long cur = stack.pop();
            if (cur < 0) {
                min = (int)(min - cur);
            }
        }
        
        public int top() {
            if (stack.peek() < 0L) {
                return min;
            } else {
                return (int)(min + stack.peek());
            }
        }
        
        public int getMin() {
            return min;   
        }
    }
    
    /**
     * 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();
     */
    

     另一种办法

    分析:保存到目前为止的最小值。当出栈导致更新最小值时,如果记录了之前的最小值信息,就能更新到pop出当前元素后的最小值。

    public class MinStack {
        Stack<Integer> stack;
        int min;
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<Integer>();
            min = Integer.MAX_VALUE;
        }
        
        public void push(int x) {
            if (x <= min) { //这里要用<=。如果x == min,x也会被判断为新的最小值,在pop时连pop两次,因此此时也要push两次
                stack.push(min);
                min = x;
            }
            stack.push(x);
        }
        
        public void pop() {
            int cur = stack.pop();
            if (cur == min) {
                min = stack.pop();
            }
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int getMin() {
            return min;
        }
    }
    
    /**
     * 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();
     */
    
  • 相关阅读:
    [转]Android输入法框的梳理
    [转]Android中OptionMenu的使用
    [转] Android把view的画面转换为bitmap
    Health Level Seven International (HL7)
    [转]andriod的apk文件相关的编译反编译工具
    [转]Android优秀开源项目收集
    而立之年 独立自主
    [转]Android模拟键盘和键盘监听的一些调研
    [转] linux中如何能在DDMS中打开真机中的数据库
    [转]关于使用SurfaceFligner进行绘图的具体实现方法
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/7169507.html
Copyright © 2011-2022 走看看