Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
解题思路一:
之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大。如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到规模变为2为止,空间开销就会小很多。JAVA实现如下:
static public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0)
return null;
if (lists.length == 1)
return lists[0];
if (lists.length == 2)
return mergeTwoLists(lists[0], lists[1]);//参考Java for LeetCode 021 Merge Two Sorted Lists
else {
ListNode[] halfLists = new ListNode[lists.length / 2];
if (lists.length % 2 == 0)
for (int i = 0; i < halfLists.length; i++)
halfLists[i] = mergeTwoLists(lists[2 * i], lists[2 * i + 1]);
else {
for (int i = 0; i < halfLists.length; i++)
halfLists[i] = mergeTwoLists(lists[2 * i], lists[2 * i + 1]);
halfLists[0] = mergeTwoLists(halfLists[0],lists[lists.length - 1]);
}
return mergeKLists(halfLists);
}
}
解题思路二:
采用优先级队列,每次将List[]中的元素入队,然后让最小的元素出队,注意Comparator的重写,JAVA实现如下:
public ListNode mergeKLists(ListNode[] lists) {
Queue<ListNode> queen = new PriorityQueue<ListNode>(
0,new Comparator<ListNode>() {//提交的时候不能加初始容量(第一个参数),否则报错
public int compare(ListNode l1, ListNode l2) {
return l1.val - l2.val;
}
});
ListNode result = new ListNode(0), index = result,poll;
for (ListNode list : lists)
if (list != null)
queen.add(list);
while (!queen.isEmpty()) {
poll=queen.poll();
index.next = new ListNode(poll.val);
index = index.next;
if (poll.next != null)
queen.add(poll.next);
}
return result.next;
}
Java for LeetCode 023 Merge k Sorted Lists的更多相关文章
- LeetCode 023 Merge k Sorted Lists
题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...
- 蜗牛慢慢爬 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】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 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】023. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- [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, 将其进行 ...
- [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 ...
随机推荐
- Cocos2d-X3.0 刨根问底(二)----- 从HelloWorld开始
小鱼习惯直接从代码实例来学习一套成型的引擎库. 运行cpp-empty-test 一个典型的HelloWorld程序翻看代码结构 看到了 main.h与main.cpp文件就从这里开始 #ifndef ...
- OKR详解及其实施
这阵子大家一直在讨论Google的绩效考核方法OKR,我们发现很多文章和说法存在错误和误导,现将其来龙去脉,操作步骤,操作技巧阐述如下,供大家参考. OKR全称是Objectives and Key ...
- visual studio 2012如何彻底删除TFS上的团队项目
http://www.cnblogs.com/zfanlong1314/p/3378441.html 本人的TFS地址:https://zfanlong1314.visualstudio.com/ 最 ...
- WGS84、Web墨卡托、火星坐标、百度坐标互转
转自:1.http://blog.csdn.net/wildboy2001/article/details/12031351 2.http://kongxz.com/2013/10/wgs-cgj/ ...
- --hdu 2570 迷瘴(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2570 Ac code: #include<stdio.h> #include<std ...
- hdu 1257 最少拦截系统(贪心)
解题思路:[要充分理解题意,不可断章取义] 贪心:每个防御系统要发挥其最大性能, 举例: Input : 9 389 207 155 300 299 170 155 158 65 Output: 2 ...
- WebBrowser 禁用右键
禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...
- [Socket网络编程]由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
原文地址:http://blog.sina.com.cn/s/blog_70bf579801017ylu.html,记录在此方便查看 解决办法: MSDN的说明: Close 方法可关闭远程主机连接, ...
- ASP.Net 5 上传文件通过虚拟路径存储
先贴上代码 [HttpPost] public IActionResult ImportTeaching(IFormFile file) { string root = @"Temp/tea ...
- ssi技术初探
http://blog.sina.com.cn/s/blog_765941620100wiir.html