Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
class MinStack {
private Stack <Integer> stack = new Stack<Integer>();
private Stack <Integer> minStack = new Stack<Integer>();
public void push(int x) {
if(minStack.isEmpty()||x<=minStack.peek())
minStack.push(x);
stack.push(x);
}
public void pop() {
if(stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
原文:http://blog.csdn.net/zhuangjingyang/article/details/41171433