题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.*;
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
public void push(int node) {
stack1.push(node);
if (stack2.isEmpty() || stack2.peek() >= node) {
stack2.push(node);
System.out.println("stack2新增"+stack2.peek());
}
}
public void pop() {
if (stack1.pop().equals(stack2.peek())) {
System.out.println(stack2.peek());
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int min() {
return stack2.peek();
}
}
if (stack1.pop().equals(stack2.peek())) {
System.out.println(stack2.peek());stack2.pop();
}
**用==运行出错,得用equals
查看29道真题和解析