题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.*; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public void pop() { stack1.pop(); } public int top() { return stack1.peek(); } public int min() { List<Integer> list = new ArrayList<>(); while (!stack1.isEmpty()) { Integer pop = stack1.pop(); stack2.push(pop); list.add(pop); } while (!stack2.isEmpty()) { stack1.push(stack2.pop()); } return Collections.min(list); } }