[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立会报告+燃尽图(十月二十日总第十一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- 60行代码:Javascript 写的俄罗斯方块游戏
哈哈这个实在是有点意思 备受打击当初用java各种类写的都要几百行啦 先看效果图: 游戏结束图: javascript实现源码: [javascript] view plaincopyprint? & ...
- EF 联合查询
EF 文章表和标签表联合查询标签id在dis中的文章,还不知道性能如何 var query = tagRepo.Entities.Include("Tags").Where(t = ...
- SQL之case when then用法详解
case具有两种格式.简单case函数和case搜索函数. <span style="font-size:14px;">--简单case函数 case sex when ...
- 1014 我的C语言文法定义与C程序推导过程
程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> < ...
- 初探Android动画之门
原文地址:http://www.cnblogs.com/kross/p/3376451.html 最近自学了下动画的相关知识,总结为今天的文章,希望对大家有帮助. Android中的动画大致分为三种: ...
- 解决 Package test is missing dependencies for the following libraries: libcrypto.so.1.0.0
根据项目要求需要用到openssl这个库,看了看编译环境幸好本身就集成了该库.但在编译openssl的功能时,碰到缺少类库的错误. Package test is missing dependenci ...
- js & 快捷键
js & 快捷键 demo js-keyboard-shortcuts.html https://codepen.io/webgeeker/pen/MLYrNq let isCtrl = fa ...
- linux下sublime text 3安装到配置
1. Sublime Text 3的下载安装 到官方网站上http://www.sublimetext.com/3下载64位(系统位64位)的.deb安装包(http://c758482.r82.cf ...
- 【uoj#310】[UNR #2]黎明前的巧克力 FWT
题目描述 给出 $n$ 个数,从中选出两个互不相交的集合,使得第一个集合与第二个集合内的数的异或和相等.求总方案数. 输入 第一行一个正整数 $n$ ,表示巧克力的个数.第二行 $n$ 个整数 $a_ ...