Stack<Integer> dataStk = new Stack<>();
Stack<Integer> minStk = new Stack<>();
public void push(int node) {
dataStk.push(node);
if (minStk.isEmpty()) {
minStk.push(node);
}else if (minStk.peek() < node){
minStk.push(minStk.peek());
}else {
minStk.push(node);
}
}
public void pop() {
if (!dataStk.isEmpty()){
dataStk.pop();
}
if (!minStk.isEmpty()){
minStk.pop();
}
}
public int top() {
return dataStk.peek();
}
public int min() {
return minStk.peek();
}
30 包含min函数的栈
最新推荐文章于 2022-03-25 23:45:28 发布