Implement a stack with min() function, which will return the smallest number in the stack.
It should support push, pop and min operation all in O(1) cost.
Solution: 用两个stack。push时,minstack push Math.min(minStack.peek(), number)
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
}
public void push(int number) {
stack.push(number);
if(minStack.isEmpty()){
minStack.push(number);
} else{
minStack.push(Math.min(minStack.peek(), number));
}
}
public int pop() {
minStack.pop();
return stack.pop();
}
public int min() {
return minStack.peek();
}