标签:subject sub code 包含min函数的栈 数据结构 元素 cep java top
import java.util.Stack; public class Solution { private Stack<Integer> stackData=new Stack<>(); private Stack<Integer> stackMin=new Stack<>(); public void push(int node) { if(stackMin.isEmpty()){ stackMin.push(node); }else if(node<min()){ stackMin.push(node); } stackData.push(node); } public void pop() { if(stackData.isEmpty()){ throw new RuntimeException("the stack is empty"); } int value=stackData.pop(); if(value==min()){ stackMin.pop(); } } public int top() { if(stackData.isEmpty()){ throw new RuntimeException("the stack is empty"); } return stackData.peek(); } public int min() { if(stackMin.isEmpty()){ throw new RuntimeException("the stack is empty"); } return stackMin.peek(); } }
标签:subject sub code 包含min函数的栈 数据结构 元素 cep java top
原文地址:https://www.cnblogs.com/chanaichao/p/10158793.html