题解 | #包含min函数的栈#
包含min函数的栈
http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
双栈实现
import java.util.Stack; public class Solution { private Stack<Integer> stack1 = new Stack<>(); private Stack<Integer> stack2 = new Stack<>(); public void push(int node) { if (stack1.isEmpty()) { stack2.push(node); } else if (node < stack2.peek()) { stack2.push(node); } else { stack2.push(stack2.peek()); } stack1.push(node); } public void pop() { stack1.pop(); stack2.pop(); } public int top() { return stack1.peek(); } public int min() { return stack2.peek(); } }