题目

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;
}
};

GitHub测试程序源码

LeetCode(23)Merge k Sorted Lists的更多相关文章

  1. 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 ...

  2. leetcode第22题--Merge k Sorted Lists

    problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...

  3. Leetcode(23)-合并K个排序链表

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [   1->4->5,   1->3->4,   2->6 ] 输出: 1-&g ...

  4. 蜗牛慢慢爬 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 ...

  5. 23. Merge k Sorted Lists - LeetCode

    Question 23. Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 ...

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

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

  7. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  8. 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 ...

  9. Merge k Sorted Lists——分治与堆排序(需要好好看)

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

随机推荐

  1. [摘录]全面学习GFW

    转载自:https://cokebar.info/archives/253 GFW会是一个长期的存在.要学会与之共存,必须先了解GFW是什么.做为局外人,学习GFW有六个角度.渐进的来看分别是: 首先 ...

  2. spring之控制反转

    IOC (Inversion of Control) 控制反转 我的理解:将创建对象的控制权从代码本身转交给了外部容器(spring容器). 1.将组件对象(业务对象)的控制权从代码本身转移到外部容器 ...

  3. wpf ComboBox的SelectionBoxItem相关依赖属性

    以前没有注意SelectionBoxItem相关依赖属性,这几天看wpf源码 特意研究了一番 <Style x:Key="ComboBoxStyle1" TargetType ...

  4. Python使用selenium进行爬虫(一)

    JAVA爬虫框架很多,类似JSOUP,WEBLOGIC之类的爬虫框架都十分好用,个人认为爬虫的大致思路就是: 1.挑选需求爬的URL地址,将其放入需求网络爬虫的队列,也可以把爬到的符合一定需求的地址放 ...

  5. HDU 3530Subsequence(单调队列)

    题意 题目链接 给出$n$个数,找出最长的区间,使得区间中最大数$-$最小数 $>= m$ 且$<= k$ Sol 考虑维护两个单调队列. 一个维护$1 - i$的最大值,一个维护$1 - ...

  6. ABAP事件分类

    1.报表事件 INITIALIZATION. START-OF-SELECTION. END-OF-SELECTION. 2.选择屏幕事件 在INITIALIZATION和START-OF-SELEC ...

  7. ubuntu下安装ffmpeg扩展

    可通过PPA进行安装 sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get ...

  8. Python3+selenium3环境搭建笔记

    系统:win7 64位浏览器:ie9 64位 chrome70 32位 firefox63 64位python版本:3.6.5 Windows x86 executable installersele ...

  9. [windows]窗口文件夹中使用常见任务

    文件夹中使用常见任务,如截图所示增加红色框部分. 设置步骤: 我的电脑--〉右键--〉属性--〉高级选项--〉性能设置--〉自定义:勾选在文件夹中使用常见任务.

  10. AutoHotKey设置ide的光标功能键

    CapsLock:: SetCapsLockState,off ;锁定为小写 CapsLock & d:: flag_C=1 ;小写d代替control键,实现跨词移动CapsLock &am ...