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

解题思路:

1、先取出k个list的首元素,每个首元素是对应list中的最小元素,组成一个具有k个结点的最小堆;o(k*logk)

2、此时堆顶元素就是所有k个list的最小元素,将其pop出,并用此最小元素所在list上的下一个结点(如果存在)填充堆顶,并执行下滤操作,重新构建堆。o(logk)

3、不断执行第二步,直到堆中元素为0。o(nlogk)

最终时间复杂度为o(nlogk),k表示list的个数,n表示所有list总结点数;

解题方法1:

使用C++ make_heap,push_heap,pop_heap函数;

步骤:

1、新建一个结点preHead,指向最终返回结果链表的首结点;新建一个curNode指针,用于操作新建链表;

2、将每个list的首指针装入一个新建的vector v中,注意判断list不为空;

3、调用make_heap函数将这些首指针构造成堆;需要编写greater比较函数;

4、循环操作,只要v不为空:

  (1)取出堆顶,放入新建链表的末端;

  (2)pop_heap,交换当前堆首尾元素,并下滤首元素;

  (3)pop_back,删除v最后一个元素(当前最小元素);

  (4)push_back,将当前最小元素的下一个元素加入堆尾;

  (5)push_heap,对堆尾元素进行上滤;

5、返回preHead->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) { //make_heap
ListNode prehead();
ListNode *curNode = &prehead;
vector<ListNode*> v;
for(int i = ; i < lists.size(); i++){
if (lists[i])
v.push_back(lists[i]);
}
make_heap(v.begin(), v.end(), heapComp); //vector -> heap data strcture while(v.size() > ){
curNode->next = v.front();
pop_heap(v.begin(), v.end(), heapComp);
v.pop_back();
curNode = curNode->next;
if(curNode->next) {
v.push_back(curNode->next);
push_heap(v.begin(), v.end(), heapComp);
}
} return prehead.next;
} static bool heapComp(ListNode* a, ListNode* b) {
return a->val > b->val;
}
};

解题方法2:

使用C++ priority_queue 构建最小堆;

步骤和上述基本一致;

代码

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
struct cmp {
bool operator()(const ListNode *n1, const ListNode *n2) {
return n1->val > n2->val;
}
}; ListNode *mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode*, vector<ListNode *>, cmp > heap;
ListNode pre_head();
ListNode *curNode = &pre_head;
for (int i = ; i < lists.size(); i++) {
if (lists[i])
heap.push(lists[i]);
} while (!heap.empty()){
curNode->next = heap.top();
heap.pop();
curNode = curNode->next;
if (curNode->next)
heap.push(curNode->next);
} return pre_head.next;
}
};

【Leetcode】【Hard】Merge k Sorted Lists的更多相关文章

  1. 【leetcode】Merge k Sorted Lists

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

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

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

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

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

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

  6. LeetCode: Merge k Sorted Lists 解题报告

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

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

  8. LeetCode 023 Merge k Sorted Lists

    题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...

  9. 23. Merge k Sorted Lists - LeetCode

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

  10. Merge k Sorted Lists

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

随机推荐

  1. IOS App Integrate Google Map Problems and Method to solve them

    1. You must get a key in google developer center, and register it in below function in AppDelegate.m ...

  2. zookeeper节点失效重连机制

    http://www.blogjava.net/xylz/archive/2011/12/05/365578.html http://blog.csdn.net/tswisdom/article/de ...

  3. 使用Ganglia监控hadoop、hbase

    Ganglia是一个监控服务器,集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标. Ganglia的强大在于:g ...

  4. mybatis like的用法

    oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 SELECT * FROM user WHERE nam ...

  5. 现成的HTML5框架

    1>sencha-touch 2>phoneGap 3>jQuery mobile 4>bootstrap

  6. LoadRunner ---检查点

    判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的HTTP状态为 200 OK ,那么VuGen 就认为脚本正确地运行了,并且是运行通过的.在绝大多数系统出错时会返回错误页面码? 不 ...

  7. android之存储篇_SQLite数据库_让你彻底学会SQLite的使用

    SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中 ...

  8. Unity(二)生命周期LifetimeManager

    描述:Unity的生命周期是注册的类型对象的生命周期,而Unity默认情况下会自动帮我们维护好这些对象的生命周期,我们也可以显示配置对象的生命周期,Unity将按照配置自动管理. //创建一个Unit ...

  9. 查看lock

    select a.session_id,a.oracle_username, a.os_user_name, b.owner "OBJECT OWNER", b.object_na ...

  10. LeetCode(一)

    数据流的中位数 class MedianFinder { private Queue<Integer> maxHeap = new PriorityQueue(new Comparator ...