题目:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.size()==) return NULL;
return Solution::mergeK(lists, , lists.size()-);
}
static ListNode* mergeK(vector<ListNode*>& lists, int begin, int end )
{
if ( begin==end ) return lists[begin];
if ( (begin+)==end ) return Solution::mergeTwo(lists[begin], lists[end]);
int mid = ( begin + end ) / ;
ListNode *firstHalf = Solution::mergeK(lists, begin, mid);
ListNode *secondHalf = Solution::mergeK(lists, mid+, end);
return Solution::mergeTwo(firstHalf, secondHalf);
}
static ListNode* mergeTwo( ListNode *h1, ListNode *h2 )
{
ListNode dummy(-);
ListNode *p = &dummy;
while ( h1 && h2 ){
if ( h1->val<h2->val ){
p->next = h1;
h1 = h1->next;
}
else{
p->next = h2;
h2 = h2->next;
}
p = p->next;
}
p->next = h1 ? h1 : h2;
return dummy.next;
}
};

tips:

多路归并写法。

=================================================

第二次过这道题,第一次没有写成归并的形式,结果超时了。

在网上查了一下这道题的时间复杂度分析:http://www.cnblogs.com/TenosDoIt/p/3673188.html

改成了归并的写法,AC了。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists)
{
return Solution::divide(lists, , lists.size()-);
}
ListNode* divide(vector<ListNode*>& lists, int begin, int end)
{
if ( begin>end ) return NULL;
if ( begin==end ) return lists[begin];
int mid = (begin+end)/;
ListNode* l = Solution::divide(lists, begin, mid);
ListNode* r = Solution::divide(lists, mid+, end);
return Solution::merge2Lists(l, r);
}
static ListNode* merge2Lists(ListNode* p1, ListNode* p2)
{
ListNode head();
ListNode* p = &head;
while ( p1 && p2 )
{
if ( p1->val < p2->val )
{
p->next = p1;
p1 = p1->next;
}
else
{
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
p->next = p1 ? p1 : p2;
return head.next;
}
};

用非递归又写了一个。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists)
{
if ( lists.empty() ) return NULL;
int length = lists.size();
while ( length> )
{
int index = ;
int i=;
for ( ; i<length; i=i+)
{
lists[index++] = Solution::merge2Lists(lists[i], lists[i-]);
}
if ( length & ) lists[index++] = lists[i-];
length = index;
}
return lists[];
}
static ListNode* merge2Lists(ListNode* p1, ListNode* p2)
{
ListNode head();
ListNode* p = &head;
while ( p1 && p2 )
{
if ( p1->val < p2->val )
{
p->next = p1;
p1 = p1->next;
}
else
{
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
p->next = p1 ? p1 : p2;
return head.next;
}
};

=====================================================

之前两次过这道题,即使看了上面提到blog的解释,也没太直观理解为什么归并的效率高。

第三次过,有点儿顿悟了:其实就是插入排序和归并排序的效率区别;只不过这次归并的是链表

【Merge K Sorted Lists】cpp的更多相关文章

  1. leetcode 【 Merge k Sorted Lists 】python 实现

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  2. 【Merge Two Sorted Lists】cpp

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  3. leetcode 【 Merge Two Sorted Lists 】 python 实现

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  4. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  5. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  6. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  7. 71. Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  9. LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists

    1. Merge Two Sorted Lists 题目链接 题目要求:  Merge two sorted linked lists and return it as a new list. The ...

随机推荐

  1. c#使用DocX添加多级标题

    博客转移到 http://jacean.github.io/ 继续分享编程经验 先上效果.可以生成多级标题,但是不能生成1,1.1,1.2这样的自动序列, 只是这样的效果. 实现方法是给Paragra ...

  2. Vue.js常见问题

    1.Vuejs组件 vuejs构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue.c ...

  3. SQL Server编程(04)基本语法

    一.定义变量 --简单赋值 declare @a int set @a=5 print @a   --使用select语句赋值 declare @user1 nvarchar(50) select @ ...

  4. (转)浅谈HTML5与css3画饼图!

    神马系饼图? 饼图,大家都应该熟知,在统计数据对比方面,几乎处处用到.如cnzz的统计饼图 从饼图中,很形象地展示了访问者地区的分布,以扇形为块的方式拼成一个大圆. 都使用什么方法实现 目前众多站点制 ...

  5. C++读入两个参数

    题目内容:编写程序计算两个整数的差. 输入描述:输入数据含有不超过50个整数对,每个整数队及每对整数的运算结果都不会超过231或-231. 输出描述:对于每次读入的一对整数,输出前者减去后者的差.每个 ...

  6. 浅析python的string.Template

    摘自:python参考手册. string模块定义了一种新字符串类型Template,简化了特定的字符串置换操作, Template定义一个类 1.template(s),  #s是字符串 s='he ...

  7. Web Design:给实验室UI们的一堂课(上)

    实验室的UI越来越水,设计什么的做的一塌糊涂,所以拖了很久,就想给他们讲一下设计或者说入门吧,上周末才倒出来时间. 这里放上PPT和讲稿吧,懒得去整理板式了. 主要讲了一下Web Design怎么做, ...

  8. 安装SRILM

    参考博文:Ubuntu 64位系统下SRILM的配置详解 来源52nlp www.52nlp.cn 首先下载SRILM 解压缩到home即可 然后需要修改MakeFile文件: # SRILM = / ...

  9. 网络开发库从libuv说到epoll

    引言 这篇博文可能有点水,主要将自己libuv的学习过程和理解. 简单谈方法. 有点杂. 那我们开始吧. 首先介绍 githup . 这个工具特别好用. 代码托管. 如果不FQ可能有点卡. 但是应该试 ...

  10. ode.js 版本控制 nvm 和 n 使用 及 nvm 重启终端失效的解决方法

    今天的话题包括2个部分 node.js 下使用 nvm 或者 n 来进行版本控制 nvm 安装node.js 版本后,重启终端 node , npm 环境变量失效 第一部分 用什么来管理 node.j ...