【Merge K Sorted Lists】cpp
题目:
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的更多相关文章
- leetcode 【 Merge k Sorted Lists 】python 实现
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 【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 ...
- 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 ...
- 【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
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- 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 ...
随机推荐
- 消息推送之GCM
利用GCM进行消息推送 原理 1.接收端向GCM注册registerid 2.发送端发消息给GCM服务器 这个过程需要三个参数: (1)API Key (2)registerid (3)传递的数据 3 ...
- C#使用结构来传递多个参数
当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } clas ...
- html判断IE版本
html判断IE版本 . <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> . <!--[if IE]> ...
- Sliverlight中PagedCollectionView的使用
最近项目中一直在和PagedCollectionView这个类打交道.通过它,我们可以以分页的形式自动处理并显示集合中的片段,尤其是和Pager控件配合的时候更能彰显其威力. PagedColecti ...
- php实现显示网站运行时间-秒转换年月日时分秒
<?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); /** * 秒转时间,格式 年 月 日 时 分 秒 * * @author w ...
- android在程序中打开另一个程序
在开发android应用的时候,在一些情况下要有前置条件,比如这边所说的要启动时要确保别的应用程序服务已经打开 或者在操作中启动别的应用等. 先来一段google上的代码: 1. 已知包名和类名的情 ...
- 压力测试之TCPP
1.下载源码 tpcc-mysql-src.tgz 2.解压 tpcc-mysql-src.tgz 3.安装 [root@DBMysql mysql]# cd /home/mysql/tpcc-mys ...
- python基础学习笔记第四天 list 元祖 字典
一 LIST方法 列表操作包含以下函数:1.cmp(list1, list2):比较两个列表的元素 2.len(list):列表元素个数 3.max(list):返回列表元素最大值 4.min(lis ...
- 【Inno Setup】 Inno Setup 64位安装程序默认安装路径
在脚本中加入: ArchitecturesInstallIn64BitMode=x64 ArchitecturesAllowed=x64
- EMVTag系列12《卡片内部风险管理数据》
Ø 9F53 连续脱机交易限制数(国际-货币) L: 1 -C(有条件):如果执行国际-货币频度检查 PBOC专有数据元. 不使用指定应用货币的连续脱机交易次数最大数,超过后交易请求联机 模板 ...