双栈法:一个栈记录正常入栈、出栈操作,另外一个栈记录当前栈情况的最小值
import java.util.Stack;
public class Solution {
int min = Integer.MIN_VALUE;
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public void push(int node) {
stack1.push(node);
if(stack2.isEmpty()){
stack2.push(node);
}else{
if(stack2.peek() < node){
stack2.push(stack2.peek());
}else{
stack2.push(node);
}
}
}
public void pop() {
stack1.pop();
stack2.pop();
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}