import java.util.Stack;
public class Solution {
//借助两个栈,一个存放所有元素,一个存放比较的元素
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
public void push(int node) {
stack1.push(node);
if (stack2.size()==0){
stack2.push(node);
}else {
int min;
//栈顶元素跟这个node进行判断
if (stack2.peek()<node){
min=stack2.peek();
}else {
min=node;
}
stack2.push(min);
}
}
public void pop() {
stack1.pop();
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
很多数据结构算法问题都会借助到一个辅助的数据结构,例如本题。