[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个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...
随机推荐
- SVN服务器搭建及客户端配置
为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subvers ...
- 王者荣耀交流协会第三次Scrum立会
会议时间:2017年10月22号 18:00-18:32,时长32分钟. 会议地点:中快餐厅二楼第二排倒数第二个桌子. 立会内容: 1.每位同学汇报了今日工作. 2.通过讨论我们决定用存excel ...
- 智能客服 对话实现--python aiml包
利用了python的aiml包进行应答 什么是AIML? AIML是Richard Wallace开发的. 他开发了一个叫A.L.I.C.E(Artificial Linguistics Intern ...
- C++:this指针的简单理解
一.什么是this指针 要想理解什么是this指针,首先必须理解在C++中是如何为类的对象分配内存空间的. #include<iostream> using namespace std; ...
- ASP.NET Core 中的 Razor 页面介绍
标题:ASP.NET Core 中的 Razor 页面介绍 地址:https://docs.microsoft.com/zh-cn/aspnet/core/razor-pages/index?view ...
- 第207天:HTTP协议头字段详解大全
本篇重点介绍一下HTTP常用的Header HTTP Header非常之多,很少有人能完全分清这些Header到底是干什么的.鉴于RFC文件规范艰深晦涩难懂,本文对协议规范中列出的HTTP Heade ...
- @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
@Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值
- BZOJ5091 摘苹果(概率期望)
大胆猜想每一步都相当于是第一步.稍微验证一下发现是对的.就做完了. #include<iostream> #include<cstdio> #include<cmath& ...
- BZOJ4719 NOIP2016天天爱跑步(线段树合并)
线段树合并的话这个noip最难题就是个裸题了. 注意merge最后return x,以及如果需要区间查询的话这里还需要up,无数次死于这里. #include<iostream> #inc ...
- CIR,CBS,EBS,PIR,PBS 名词解释 令牌桶应用
为了达到上述目的,我们需要对进入网络的流量进行监督,实现CAR(Committed Access Rate). CAR:将进入网络的用户流量的速率限制在约定的范围之内,从而避免引起网络拥塞. CIR( ...