Stack comprising the min function (to prove safety offer_30)

Title Description

Stack data structure definition, implement this type can be a min function stack smallest elements.

 

  1. Stack.peek()

    peek () function returns the top of the stack elements, but does not pop the top element.

  2. Stack.pop()

    pop()函数返回栈顶的元素,并且将该栈顶元素出栈。
public class Solution {

    private Stack<Integer> dataStack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();
    public void push(int node) {
        dataStack.push(node);
        minStack.push (minStack.isEmpty () ? node: Math.min (minStack.peek (), Node));
    }
    
    public void pop() {
        dataStack.pop();
        minStack.pop ();
    }
    
    public int top() {
        return dataStack.peek();
    }
    
    public  int min () {
         return minStack.peek ();
    }
}

Guess you like

Origin www.cnblogs.com/ziytong/p/12150179.html