标签:
//思路:入栈时不是最小值,永远都没机会成为最小值。
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(); } }
标签:
原文地址:http://www.cnblogs.com/yueyebigdata/p/5060723.html