AC but why when I use LinkedList in this way it tells me TML?


  • 0
    D

    Stack<Integer> stack = new Stack<Integer>();
    Stack<Integer> minStack = new Stack<Integer>();
    public void push(int x) {
    stack.push(x);
    if(minStack.isEmpty()||minStack.peek()>=x){
    minStack.push(x);
    }
    }

    public void pop() {
       if(stack.isEmpty()){
           return;
       }
        if((!minStack.isEmpty())&&minStack.peek()>=stack.peek()){
            minStack.pop();
        }
         
            stack.pop();
        
    }
    
    public int top() {
        if(!stack.isEmpty())
         return stack.peek();
         return 0;
    }
    
    public int getMin() {
      return minStack.peek();
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.