Min Stack:实现能返回最小值的栈

来源:互联网 发布:淘宝刷单降权恢复成功 编辑:程序博客网 时间:2024/06/07 01:07

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.

思路一:用数组当做栈,然后压栈时记录最小值,出栈时若弹出了最小值,则重新搜索最小值。效率低。

class MinStack {    int minindex = -1;    int top = -1;    int[] stack;    /** initialize your data structure here. */    public MinStack() {        stack = new int[1000000];    }        public void push(int x) {        stack[++top] = x;        if(minindex==-1){            minindex = top;        }else{            if(x<stack[minindex]){                minindex = top;            }        }    }        public void pop() {        if(top>=0){            top--;        }        //System.out.println(top+"t + m"+minindex);        if(minindex>top){            if(top==-1){                minindex = -1;            }else{                int min = Integer.MAX_VALUE;                for(int i = 0;i<=top;i++){                    if(stack[i]<=min){                        minindex  = i;                        min = stack[i];                    }                }            }        }       //System.out.println(top+"t + m"+minindex);    }        public int top() {        return stack[top];    }        public int getMin() {        return stack[minindex];    }}/** * 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元素小于零,则说明与前一阶段的最小值相比出栈元素更小,所以前一阶段最小值应该是当前最小值-差值,即min = min-stack.pop()。
class MinStack {    Stack<Long> stack;    long min = 0;    /** initialize your data structure here. */    public MinStack() {        stack = new Stack<Long>();    }        public void push(int x) {        if(stack.isEmpty()){            stack.push(0L);            min = x;        }else{            stack.push(x-min);//先存的差值        }        if(x-min<0){//再更新的最小值            min = x;        }    }        public void pop() {        if(!stack.isEmpty()){            long temp = stack.pop();            if(temp<0){//所以当出栈元素小于零时,表示出栈元素是当前整个栈的最小值,需要更新出栈后的栈内最小值。                min = min - temp;//由于入栈时过程是:若此元素x是最小值push(x - min), min = x;所以倒过来: preMIN = nowMIN - temp,其中nowMIN表            }   //示未将元素差值temp出栈时整个栈的最小元素,preMIN表示将temp出栈后栈内最小元素。即min = min - temp。         }            }        public int top() {        long top=stack.peek();        if (top>0){//表示栈顶元素非最小值,根据最小值还原即可            return (int)(top+min);        }else{//表示栈顶元素是最小值,返回最小值           return (int)(min);        }                      }        public int getMin() {        return (int)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(); */



阅读全文
0 0
原创粉丝点击