leetcode -- Merge k Sorted Lists add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
[解题思路]
以前的解法的时间复杂度过高,通过在网上搜索,得到优化的时间复杂度:O(n*lgk)
维护一个大小为k的最小堆,每次得到一个最小值,重复n次
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head = null;
int len = lists.size();
if(len <= 1)
return null;
head = merge2List(lists.get(0), lists.get(1));
for(int i = 2; i < len; i++){
head = merge2List(lists.get(i), head);
}
return head; } public ListNode merge2List(ListNode node1, ListNode node2){
ListNode head = null;
ListNode tmp = head;
while(node1 != null && node2 != null){
if(node1.val <= node2.val){
ListNode node = new ListNode(node1.val);
tmp = node;
tmp = tmp.next;
} else {
ListNode node = new ListNode(node2.val);
tmp = node;
tmp = tmp.next;
}
node1 = node1.next;
node2 = node2.next;
} while(node1 != null){
ListNode node = new ListNode(node1.val);
tmp = node;
tmp = tmp.next;
node1 = node1.next;
} while(node2 != null){
ListNode node = new ListNode(node2.val);
tmp = node;
tmp = tmp.next;
node2 = node2.next;
}
return head; }
}
上一版本有bug,修复如下:
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode head = null;
int len = lists.size();
if(len == 0)
return null;
else if(len == 1){
return lists.get(0);
}
head = merge2List(lists.get(0), lists.get(1));
for(int i = 2; i < len; i++){
head = merge2List(lists.get(i), head);
}
return head; } public ListNode merge2List(ListNode node1, ListNode node2){
ListNode head = new ListNode(Integer.MIN_VALUE);
ListNode tmp = head;
while(node1 != null && node2 != null){
if(node1.val <= node2.val){
ListNode node = new ListNode(node1.val);
tmp.next = node;
tmp = tmp.next;
node1 = node1.next;
} else {
ListNode node = new ListNode(node2.val);
tmp.next = node;
tmp = tmp.next;
node2 = node2.next;
}
} if(node1 != null){
tmp.next = node1;
} if(node2 != null){
tmp.next = node2;
}
head = head.next;
return head; }
}
http://blog.csdn.net/zyfo2/article/details/8682727
http://tech-wonderland.net/blog/leetcode-merge-k-sorted-lists.html
leetcode -- Merge k Sorted Lists add code的更多相关文章
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
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 linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- Leetcode:Merge k Sorted Lists分析和实现
题目大意是传入一个链表数组lists,每个链表都由若干个链接的链表结点组成,并且每个链表结点记录一个整数.题目保证传入的链表中的整数按从小到大进行排序. 题目要求我们输出一个新的链表,这个链表中应该包 ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- LeetCode —— Merge k Sorted Lists
/* ** 算法的思路: ** 1.将k个链表的首元素进行建堆 ** 2.从堆中取出最小的元素,放到链表中 ** 3.如果取出元素的有后续的元素,则放入堆中,若没有则转步骤2,直到堆为空 */ #in ...
随机推荐
- codeforces 696B Puzzles 树形概率计算
题意:给一棵有根树,从根节点深搜,每次随机走,问每个点的dfs序的期望是多少 分析:对于每一个点,它的所有祖先节点dfs序肯定在它之前,它所在的子树的节点一定在它后面, 剩下的是既不是子树又不是祖先的 ...
- 提高Web页面性能的技巧
现在动辄几兆大小的页面加载量,让性能优化成了不可避免的热门话题.WEB 应用越流畅,用户体验就会越好,继而带来更多的访问量.这也就是说,我们应该反省一下那些过度美化的 CSS3 动画和多重操作的 DO ...
- 基于51,人体红外感应和RC522的门禁系统
总结一下最近学的东西,这两天学的东西,rfid门卡系统终于弄出来来了,这个程序算现在写过的比较满意的程序,大家可以参考参考 主函数: #include<reg52.h> #include& ...
- Android中Cursor(游标)类的概念和用法
使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...
- 开源框架DNN使用01
我先简单地介绍下我个人对于DNN的浅显理解吧. 我觉得对于刚接触的人来说首先理解DNN的原理,大框架是很重要的.它整个网站其实是没几个页面的,从源码上就可以看出, 一个Default页.一个Error ...
- poj1000 A+B Problem
Description Calculate a+b Input Two integer a,b (0<=a,b<=10) Output Output a+b Sample Input 1 ...
- Mahout应用(一)
Mahout应用(一) Mahout 是应用于hadoop上的数据挖掘工具(废话不多说) 这里先简单介绍一下mahout的一般使用方法. 拿kmeans为列子 Mahout中的kmeans所需要的输入 ...
- Android实例-读取设备联系人(XE8+小米2)
相关资料: http://www.colabug.com/thread-1071065-1-1.html 结果: 1.将权限打开Read contacts设置为True,不然报图一的错误. 2.搜索空 ...
- BestCoder Round #66 (div.2) hdu5592
GTW likes math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- [iOS 多线程 & 网络 - 2.4] - 大文件下载 (边下边写/暂停恢复下载/压缩解压zip/多线程下载)
A.需求 边下边写入硬盘 显示下载进度 暂停/恢复 下载 解压文件 多线程下载 B.基本知识 1.小文件下载 如果文件比较小,下载方式会比较多直接用NSData的+ (id)dataWithCon ...