[LeetCode] 23. Merge k Sorted Lists ☆☆
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
解法1:
采用递归的方法,不管合并几个,归根到底还是需要两两合并。
首先想到的是前两个先合并,然后再跟第三个合并,然后第四个。。。。但是这种做法效率不高。
换个思路,采用分治法,对数量超过2的任务进行拆分,直到最后只有一个或两个链表再进行合并。代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null; } else if (lists.length == 1) {
return lists[0]; } else {
ListNode res = new ListNode(0);
ListNode last = res; int mid = lists.length / 2;
ListNode one = mergeKLists(Arrays.copyOfRange(lists, 0, mid));
ListNode two = mergeKLists(Arrays.copyOfRange(lists, mid, lists.length)); while (one != null && two != null) {
if (one.val < two.val) {
last.next = one;
one = one.next;
} else {
last.next = two;
two = two.next;
}
last = last.next;
} last.next = one != null ? one : two;
return res.next;
} }
}
或者:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
} int n = lists.length;
while (n > 1) {
int k = (n + 1) / 2;
for (int i = 0; i < n / 2; i++) {
lists[i] = mergeTwoLists(lists[i], lists[i + k]);
}
n = k;
}
return lists[0];
} public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode head = new ListNode(0);
ListNode last = head; while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
last.next = list1;
list1 = list1.next;
} else {
last.next = list2;
list2 = list2.next;
}
last = last.next;
} last.next = list1 != null ? list1 : list2;
return head.next;
}
}
解法2:
采用小根堆的方法,先将k个链表的首节点加入堆中,每次会自动输出最小的节点,将该节点加入到最终结果的链表中,然后将其下一个元素(如果不为null)加入堆中,直到最终堆为空,即输出了全部元素。代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>() {
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1; // 这样操作的原因是,确保堆顶(最小节点)不会为null
} else if (right == null) {
return -1; // 这样操作的原因是,确保堆顶(最小节点)不会为null
} // 因为下面操作中确保堆中不会有null,所以这两个判断不要亦可
return left.val - right.val;
}
}; public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
} Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.length, ListNodeComparator);
for (ListNode node : lists) {
if (node != null) {
heap.add(node);
}
} ListNode dummy = new ListNode(0);
ListNode last = dummy;
while (!heap.isEmpty()) {
last.next = heap.poll();
last = last.next;
if (last.next != null) {
heap.add(last.next);
}
}
return dummy.next;
}
}
[LeetCode] 23. Merge k Sorted Lists ☆☆的更多相关文章
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆
转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- Java [leetcode 23]Merge k Sorted Lists
题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex ...
- [leetcode]23. Merge k Sorted Lists归并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. I ...
- [LeetCode]23. Merge k Sorted Lists合并K个排序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- leetcode 23. Merge k Sorted Lists(堆||分治法)
Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...
随机推荐
- Scrum立会报告+燃尽图 04
此作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2194 一.小组介绍 组长:王一可 组员:范靖旋,王硕,赵佳璐,范洪达,祁玉 ...
- 【Alpha】阶段第二次Scrum Meeting
[Alpha]阶段第二次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 发表评论接口 更新评论接口 赵智源 部署实际项目 编写脚本实现持续集成 肖萌威 编写注册 ...
- 第三周pspo过程文档
团队协作: 日期/任务 听课 编写程序 阅读相关书籍 日总计 周一 110 60 ...
- Right-BICEP单元测试
一.测试方法:Right-BICEP Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制错误条件发生? ...
- 让程序运行更加面向用户——电梯V2.1
电梯V2.1 GitHub仓库地址 Problem 为程序添加命令行参数(自行利用搜索引擎进行学习). 写成 .cpp .h 文件分离的形式(大多数同学已经达到). 继续完善函数分离.模块化思想. 要 ...
- HDU 5233 Gunner II 离散化
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5233 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- alpha6/10
队名:Boy Next Door 燃尽图 晗(组长) 今日完成 学习了css的一些基本操作. 明日工作 抽空把javascript的基本操作学习一下 还剩下哪些任务 微信API还有京东钱包的API. ...
- 常用IDE插件
Visual Studio 常用 Refactoring Essentials:代码重构分析 Roslynator:代码重构 CodeMaid:代码格式化 Github Extension for V ...
- lintcode-391-数飞机
391-数飞机 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 注意事项 如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权. 样例 对于每架 ...
- 运维工程师如果将web服务http专变为https
1:生成私钥 2:生成证书签署请求 3:在提供CA签署的web网站上,提交生成的证书签署请求 4:下载已经签署的CA证书 5:将证书的信息保留在web服务器中,且应用到提供web服务的 ...