思路: 需要一个辅助栈来存放每次push操作时,当前栈内元素的最小值。
public class Solution {
Stack<Integer> data_stack=new Stack<>();
Stack<Integer> min_stack=new Stack<>();
int min=Integer.MAX_VALUE;
public void push(int node) {
data_stack.push(node);
min=Math.min(min,node);
if(node<min){
min_stack.push(node);
}else{
min_stack.push(min);
}
}
public void pop() {
min_stack.pop();
data_stack.pop();
}
public int top() {
return data_stack.peek();
}
public int min() {
return min_stack.peek();
}
}