包含min函数的栈
- 时间限制:1秒空间限制:32768K
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
import java.util.Stack; public class Solution { Stack<Integer> stack =new Stack<>(); Stack<Integer> stackMin=new Stack<>(); public void push(int node) { stack.push(node); if(stackMin.isEmpty()){ stackMin.push(node); }else{ if(stackMin.peek()>node){ stackMin.push(node); }else{ stackMin.push(stackMin.peek()); } } } public void pop() { stackMin.pop(); stack.pop(); } public int top() { return stack.peek(); } public int min() { return stackMin.peek(); } }