题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.*; import java.util.Stack; public class Solution { Stack<Integer> stackVal = new Stack<Integer>(); //本地难点:node和min操作同步记录 Stack<Integer> stackMin = new Stack<Integer>(); public void push(int node) { stackVal.add(node); if (stackMin.isEmpty()) { stackMin.push(node); } else { if (node < stackMin.peek()) { stackMin.push(node); } else { stackMin.push(stackMin.peek()); } } } public void pop() { stackVal.pop(); stackMin.pop(); } public int top() { return stackVal.peek(); } public int min() { return stackMin.peek(); } }