LeetCode(23)Merge k Sorted Lists
题目
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
分析
合并k个有序链表。
我们从数据结构的链表章节学会了如何合并两个链表,针对此题,一个简单的解法便是遍历k次,两个链表合并,合并后的结果与下一个链表合并;
此时时间复杂度为O(nk),遗憾,提交会出现TLE。
所以,必须另觅他法,我们知道排序领域中很多方法都可以提高性能比如合并排序性能为O(nlogn);可以应用到此题,可以顺利AC,时间复杂度为O(nlogk)
AC代码
class Solution {
public:
/*方法一:采用合并排序的思路 复杂度为O(nlogk)*/
ListNode* mergeKLists(vector<ListNode*>& lists) {
int len = lists.size();
ListNode *head = NULL;
if (len == 0)
return head;
else if (len == 1)
{
head = lists[0];
return head;
}
else{
int mid = (len - 1) / 2;
vector<ListNode *> l1(lists.begin(), lists.begin() + mid + 1);
vector<ListNode *> l2(lists.begin() + mid + 1, lists.end());
ListNode *head1 = mergeKLists(l1);
ListNode *head2 = mergeKLists(l2);
return mergeTwoLists(head1, head2);
}//else
}
/*方法二:暴力法 复杂度为O(nK) 出现超时*/
ListNode* mergeKLists2(vector<ListNode*>& lists) {
int len = lists.size();
ListNode *head = NULL;
if (len == 0)
return head;
else if (len == 1)
{
head = lists[0];
return head;
}
else{
head = mergeTwoLists(lists[0], lists[1]);
for (int i = 2; i < len; i++)
head = mergeTwoLists(head, lists[i]);
}
return head;
}
/*合并两个链表函数*/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
//合并后链表初始化为空
ListNode *rl = NULL;
ListNode *p = l1, *q = l2;
if (l1->val <= l2->val)
{
rl = l1;
p = l1->next;
}
else{
rl = l2;
q = l2->next;
}
rl->next = NULL;
ListNode *head = rl;
while (p && q)
{
if (p->val <= q->val)
{
rl->next = p;
p = p->next;
}
else{
rl->next = q;
q = q->next;
}//else
rl = rl->next;
}//while
while (p)
{
rl->next = p;
p = p->next;
rl = rl->next;
}
while (q)
{
rl->next = q;
q = q->next;
rl = rl->next;
}
return head;
}
};
LeetCode(23)Merge k Sorted Lists的更多相关文章
- LeetCode(21)Merge Two Sorted Lists
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- leetcode第22题--Merge k Sorted Lists
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...
- Leetcode(23)-合并K个排序链表
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1-&g ...
- 蜗牛慢慢爬 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 ...
- 23. Merge k Sorted Lists - LeetCode
Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- python 中的堆 (heapq 模块)应用:Merge K Sorted Lists
堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...
- Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- Merge k Sorted Lists——分治与堆排序(需要好好看)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 1 ...
随机推荐
- node.js安装Oracledb指导文档
https://community.oracle.com/docs/DOC-931127
- HDU 5875 H - Function 用单调栈水过了
http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...
- Spring Boot Security配置教程
1.简介 在本文中,我们将了解Spring Boot对spring Security的支持. 简而言之,我们将专注于默认Security配置以及如何在需要时禁用或自定义它. 2.默认Security设 ...
- Zepto核心模块源代码分析
一.Zepto核心模块架构 Zepto核心模块架构图 该图展示了Zepto核心模块架构代码的组织方式.主要分为私有变量.函数和暴露给用户的所有api. Zepto核心模块架构代码 该图展示了Zepto ...
- Linux大棚版vimrc配置
Linux大棚版vimrc配置—V2.0版本,如下: [shell] $cat .vimrc “== “Author :roc “Website:roclinux.cn “Version:2.0 “= ...
- wpf ComboBox的SelectionBoxItem相关依赖属性
以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番 <Style x:Key="ComboBoxStyle1" TargetType ...
- Memcached分布式原理
http://younglibin.iteye.com/blog/2043761 浅显易懂,值得一读
- where whereis locate find 的用法
1.where :where ifconfig.用来搜索命令,显示命令是否存在以及路径在哪 2.whereis:whereis vim .用来搜索程序名,而且只搜索二进制文件(参数-b).man说明文 ...
- C# 对接腾讯企业邮接口----get/post请求
在无所知之的情况下.来了一个对接接口的任务,没办法,只能根据前端时候的经验硬着头皮上了,随后又整理了一下写的方法,主要包括了部门的创建.更新.删除.查找.然后他们的前提是token的获取 首先HTTP ...
- SQL Server扩展事件system_health会话总结
system_health会话概念 我们知道扩展事件(Extended Events)是从SQL Server 2008开始引入的.system_health会话是SQL Server默认包含的扩展事 ...