3.2---最小栈(CC150)
//思路:入栈时不是最小值,永远都没机会成为最小值。
import java.util.Stack; class MinStack { private Stack<Integer> stack = new Stack<Integer>(); private Stack<Integer> minStack = new Stack<Integer>(); public void push(int x) { stack.push(x); if(!minStack.empty()) { int min = minStack.peek(); if(x <= min) { minStack.push(x); } } else { minStack.push(x); } } public void pop() { if(minStack.size() != 0 && ((int)stack.peek() == (int)minStack.peek())) { minStack.pop(); } stack.pop(); } public int top() { return (int)stack.peek(); } public int getMin() { return (int)minStack.peek(); } }