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 ...
随机推荐
- 【windows核心编程】一个API拦截的例子
API拦截 修改PE文件导入段中的导入函数地址 为 新的函数地址 这涉及PE文件格式中的导入表和IAT,PE文件中每个隐式链接的DLL对应一个IMAGE_IMPORT_DESCRIPTOR描述符结构, ...
- 使用std::function 把类成员函数指针转换为普通函数指针
前言 这是改造前一篇 设计模式 的基础,使通知者不必知道观察者的类名和函数名,只需要知道更新函数的原型即可. 开发环境:WIN7 32位 + VS2010 发现在VS2005中使用std::funti ...
- IOS-day02_OC中类的声明
在上一个笔记中类的使用中,在编译链接的时候会有警告,原因就是我们没有对类进行声明 类的声明如下:使用关键字@interface #import <Foundation/Foundation.h& ...
- QT-【转】Qt 4迁移至Qt 5
将Qt 4代码迁移到Qt 5还是比较简单的.实际上,在Qt 5开发过程中就已经注意了与Qt 4代码保持兼容性. 与Qt 3到Qt 4的迁移不同,Qt 5的核心类库并没有做大的API的修改,只有几个新的 ...
- bzoj 1109 [POI2007]堆积木Klo(LIS)
[题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...
- MySQL数据库备份和还原
备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql 备份MySQL数据库 ...
- 一个考察for循环题 讨论一下
一道Java程序题,主要是考察for循环如下所示: public class Test { static boolean fun(char c) { System.out.print(c); retu ...
- JavaFx2.0中CSS的应用
http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...
- 【转】CocoaPods的安装以及遇到的坑
一.CocoaPods是什么? CocoaPods是一个用Ruby写的.负责管理iOS项目中第三方开源库的工具,CocoaPods能让我们集中的.统一管理第三方开源库,为我们节省设置和更新第三方开源库 ...
- HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...