1、前言
题目描述2、思路
3、代码
class MinStack {
private Stack<Integer> stack1 = new Stack<Integer>();
private Stack<Integer> stack2 = new Stack<Integer>();
/** initialize your data structure here. */
public MinStack() {
}
public void push(int x) {
stack1.push(x);
if(!stack2.isEmpty() && stack2.peek() < x){
return;
}
stack2.push(x);
}
public void pop() {
if(stack1.pop().equals(stack2.peek())){
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
网友评论