题解 | #包含min函数的栈#
包含min函数的栈
https://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49
import java.util.Stack;
import java.util.PriorityQueue;
public class Solution {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
Stack<Integer> stack = new Stack<Integer>();
public void push(int node) {
stack.push(node);
priorityQueue.add(node);
}
public void pop() {
priorityQueue.remove(stack.peek());
stack.pop();
}
public int top() {
return stack.peek();
}
public int min() {
return priorityQueue.peek();
}
}

查看1道真题和解析