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