用两个栈实现的,最主要是,java中的Integer的==比较操作,只有在-127-127之间才会自动转换,如果超过这个范围,==就比较的是地址了,这个错我找了好久啊,包括在eclipse实验(用的1),我还以为是版本问题。。
class MinStack {
/** initialize your data structure here. */
Stack<Integer> stack;
Stack<Integer> stack1;
public MinStack() {
stack=new Stack<Integer>();
stack1=new Stack<Integer>();
}
public void push(int x) {
if(stack.empty()||stack1.peek()>=x){
stack1.push(x);
}
stack.push(x);
}
public void pop() {
if((int)stack1.peek()==(int)stack.peek()){
stack1.pop();
}
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return stack1.peek();
}
}