JZ30 包含min函数的栈
包含min函数的栈_牛客题霸_牛客网 (nowcoder.com)
import java.util.*;
public class Solution {
Stack<Integer> st1=new Stack<>();//数据栈
Stack<Integer> st2=new Stack<>();//最小栈
public void push(int node) {
st1.push(node);
if(st2.isEmpty()||node<st2.peek()){
st2.push(node);
}else{
st2.push(st2.peek());
}
}
public void pop() {
st1.pop();
st2.pop();
}
public int top() {
return st1.peek();
}
public int min() {
return st2.peek();
}
}