堆(优先级队列 PriorityQueue)
堆(优先级队列 PriorityQueue)
堆(优先级队列 PriorityQueue)通过题目讲解题目链接博客:合并果子(优先级队列)提交代码代码语言:javascript代码运行次数:0运行复制import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
堆(优先级队列 PriorityQueue)
通过题目讲解
题目链接博客:合并果子(优先级队列)
提交代码
代码语言:javascript代码运行次数:0运行复制import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
int n = ();
for (int i = 0; i < n; ++ i)
{
q.add(());
}
int sum = 0;
while(q.size()>1)
{
int a = q.poll();
int b = q.poll();
sum += a + b;
q.add(a + b);
}
println(sum);
}
}
常用方法
和Queue一样
参考文档:队列(Queue)
遍历方式
代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) {
PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>();
pqueue.add(100);
pqueue.add(10);
pqueue.add(11);
pqueue.add(1);
pqueue.add(646);
println(pqueue);
/*[1, 10, 11, 100, 646]*/
for (Integer it : pqueue)
{
print(it + " ");
}
/*1 10 11 100 646 */
println();
Iterator<Integer> it = pqueue.iterator();
while(it.hat())
{
print(() + " ");
}
/*1 10 11 100 646 */
}
排序
代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) {
// 默认排序规则是升序排列
// 可以自定义排序规则
PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int num = 0;
if (o1 > o2) num = -1;
else num = 1;
return num;
}
});
pqueue.add(100);
pqueue.add(10);
pqueue.add(11);
pqueue.add(1);
pqueue.add(646);
println(pqueue);
/*[646, 100, 11, 1, 10]*/
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent 删除integer博客队列排序add #感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-19 23:27:31
推荐阅读
留言与评论(共有 20 条评论) |
本站网友 汇禾禧福汇 | 13分钟前 发表 |
pqueue) { print(it + " "); } /*1 10 11 100 646 */ println(); Iterator<Integer> it = pqueue.iterator(); while(it.hat()) { print(() + " "); } /*1 10 11 100 646 */ }排序代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) { // 默认排序规则是升序排列 // 可以自定义排序规则 PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1 | |
本站网友 新华苑 | 7分钟前 发表 |
1 | |
本站网友 大厦将倾独木难支 | 20分钟前 发表 |
100 | |
本站网友 空鼻综合症 | 13分钟前 发表 |
堆(优先级队列 PriorityQueue) 堆(优先级队列 PriorityQueue)通过题目讲解题目链接博客:合并果子(优先级队列)提交代码代码语言:javascript代码运行次数:0运行复制import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PriorityQueue<Integer> q = new PriorityQueue<Integer>(); int n = (); for (int i = 0; i < n; ++ i) { q.add(()); } int sum = 0; while(q.size()>1) { int a = q.poll(); int b = q.poll(); sum += a + b; q.add(a + b); } println(sum); } }常用方法和Queue一样 参考文档:队列(Queue)遍历方式代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) { PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(); pqueue.add(100); pqueue.add(10); pqueue.add(11); pqueue.add(1); pqueue.add(646); println(pqueue); /*[1 | |
本站网友 真香定律 | 28分钟前 发表 |
堆(优先级队列 PriorityQueue) 堆(优先级队列 PriorityQueue)通过题目讲解题目链接博客:合并果子(优先级队列)提交代码代码语言:javascript代码运行次数:0运行复制import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PriorityQueue<Integer> q = new PriorityQueue<Integer>(); int n = (); for (int i = 0; i < n; ++ i) { q.add(()); } int sum = 0; while(q.size()>1) { int a = q.poll(); int b = q.poll(); sum += a + b; q.add(a + b); } println(sum); } }常用方法和Queue一样 参考文档:队列(Queue)遍历方式代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) { PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(); pqueue.add(100); pqueue.add(10); pqueue.add(11); pqueue.add(1); pqueue.add(646); println(pqueue); /*[1 | |
本站网友 三国志11水浒剧本 | 6分钟前 发表 |
原始发表:2025-01-20 | |
本站网友 军人家属待遇 | 25分钟前 发表 |
原始发表:2025-01-20 | |
本站网友 国家贫困县名单 | 10分钟前 发表 |
原始发表:2025-01-20 | |
本站网友 元气大伤 | 14分钟前 发表 |
11 | |
本站网友 奇瑞销量 | 16分钟前 发表 |
如有侵权请联系 cloudcommunity@tencent 删除前往查看integer博客队列排序add | |
本站网友 亚当斯的一家 | 20分钟前 发表 |
646]*/ for (Integer it | |
本站网友 隐翅虫咬人后图片 | 10分钟前 发表 |
pqueue) { print(it + " "); } /*1 10 11 100 646 */ println(); Iterator<Integer> it = pqueue.iterator(); while(it.hat()) { print(() + " "); } /*1 10 11 100 646 */ }排序代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) { // 默认排序规则是升序排列 // 可以自定义排序规则 PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1 | |
本站网友 多大点事啊 | 25分钟前 发表 |
100 | |
本站网友 李鸣 | 7分钟前 发表 |
10]*/ }本文参与 腾讯云自媒体同步曝光计划 | |
本站网友 永和租房 | 13分钟前 发表 |
11 | |
本站网友 缓冲区 | 14分钟前 发表 |
10 | |
本站网友 随州编钟 | 8分钟前 发表 |
11 | |
本站网友 四大通讯社 | 7分钟前 发表 |
原始发表:2025-01-20 | |
本站网友 二三线城市 | 11分钟前 发表 |
堆(优先级队列 PriorityQueue) 堆(优先级队列 PriorityQueue)通过题目讲解题目链接博客:合并果子(优先级队列)提交代码代码语言:javascript代码运行次数:0运行复制import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); PriorityQueue<Integer> q = new PriorityQueue<Integer>(); int n = (); for (int i = 0; i < n; ++ i) { q.add(()); } int sum = 0; while(q.size()>1) { int a = q.poll(); int b = q.poll(); sum += a + b; q.add(a + b); } println(sum); } }常用方法和Queue一样 参考文档:队列(Queue)遍历方式代码语言:javascript代码运行次数:0运行复制public static void main(String[] args) { PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>(); pqueue.add(100); pqueue.add(10); pqueue.add(11); pqueue.add(1); pqueue.add(646); println(pqueue); /*[1 |