import java.util.PriorityQueue; public class 优先队列 { public static void main(String[] args) { // 最小堆 PriorityQueue<Integer> q = new PriorityQueue<>(10); for (int i = 0; i < 1000; i++) { int a=(int) (Math.random()*1000); if (q.isEmpty() || q.size() < 10) { q.offer(a); } else { if (q.peek() < a) { q.poll(); q.offer(a); } } } System.out.println(q); } }

相关推荐

09-26 19:45
门头沟学院 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务