辅助栈存储最小元素,与力扣(剑指 Offer 30. 包含min函数的栈)相同
import java.util.*; public class Solution { private Stack<Integer> A = new Stack<>(); private Stack<Integer> B = new Stack<>(); public void push(int node) { A.push(node); if (B.isEmpty() || B.peek() >= node) B.push(node); } public void pop() { if (B.peek().equals(A.pop())) B.pop(); } public int top() { return A.peek(); } public int min() { return B.peek(); } }