题解 | #包含min函数的栈#
包含min函数的栈
http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.Stack;
public class Solution {
Stack<Integer>normal = new Stack<Integer>();
Stack<Integer>mmin = new Stack<Integer>();
public void push(int node) {
normal.push(node);
if(mmin.isEmpty()){
mmin.push(node);
}
else{
mmin.push(Math.min(mmin.peek(),node));
}
}
public void pop() {
normal.pop();
mmin.pop();
}
public int top() {
return normal.peek();
}
public int min() {
return mmin.peek();
}
}
维护一个在相同数量时的最小值, 因为不存在对normal栈pop最小值的操作