155. Min Stack

来源:互联网 发布:北京和隆优化科技 编辑:程序博客网 时间:2024/05/23 14:03

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.
Seems easy but has lots of traps
Solution
class MinStack {    Stack<Integer> stack = new Stack<Integer>();Stack<Integer> min = new Stack<Integer>();int minv = Integer.MAX_VALUE;   /*    First version    int minValue = Integer.MAX_VALUE;    public void push(int x){        if(x < minValue){ // this should be x <= minValue             minValue = x; //don't foget this and in while loops don't forget i++            min.push(x);        }        stack.push(x)    }    this has a bug when min is empty and push a new element the min stack, right at this time minValue is the old value and cannot be updated so a new element cannot be added into the min stack   */public void push(int x) {if (min.isEmpty() || min.peek() >= x) {  min.push(x);}stack.push(x);}public void pop() {if (stack.isEmpty()) {return;}int a = stack.peek();int b = min.peek();if (a == b) { //at first I wrote if(stack.peek() == min.peek()) this does not work     min.pop(); //becuase peek() returns Integer and you cannot use == to compare the value}stack.pop();}public int top() {if (stack.isEmpty()) {return 0;}return stack.peek();}public int getMin() {if (min.isEmpty()) {return 0;}return min.peek();}}


0 0
原创粉丝点击