import java.util.Stack; public class Solution { Stack<Integer> dataStack = new Stack<>(); Stack<Integer> minStack = new Stack<>(); public void push(int node) { dataStack.push(node); if (minStack.isEmpty() || minStack.peek() > node) { minStack.push(node); } else { //放入自己 因为要保证对应dataStack中的最小元素要在minStack中存在, //一旦dataStack中a元素移除 即使a是最小的, //也要保证minStack中下一个弹出的元素应该是,dataStack中除去a下一个最小的元素 minStack.push(minStack.peek()); } } public void pop() { if (!dataStack.isEmpty()) { dataStack.pop(); } if (!minStack.isEmpty()) { minStack.pop(); } } public int top() { if (!dataStack.isEmpty()) { return dataStack.peek(); } return 0; } public int min() { if (!minStack.isEmpty()) { return minStack.peek(); } return 0; } }