Title Description
Stack data structure definition, implement this type can be a min function stack smallest elements.
-
Stack.peek()
peek () function returns the top of the stack elements, but does not pop the top element.
-
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 (); } }