Merge k Sorted Lists

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

两个方法: 方法1. 利用 STL 中的 multiset (根据结点内的值)自动对指针排序。空间 O(N), 时间 O(NlogN).

(亦可用于 k 个无序链表)。(AC: 164ms

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
bool compare (const ListNode *p1, const ListNode *p2) {
return p1->val < p2->val;
}
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
typedef bool (*cmp) (const ListNode *p1, const ListNode *p2);
multiset<ListNode*, cmp> container(compare);
for(size_t i = 0; i < lists.size(); ++i) {
ListNode *p = lists[i];
while(p) { container.insert(p); p = p->next; }
}
ListNode *head = NULL, *p = head;
for(auto it = container.begin(); it != container.end(); ++it) {
if(p) { p->next = *it; p = *it; }
else p = head = *it;
}
if(p) p->next = NULL;
return head;
}
};

方法2. 不利用任何 STL 函数。对指针建堆排序,只需要一个(win32: 4Byte)指针数组即可。空间 : O(K), 时间 O(NlogK)

(AC: 140ms)

最初代码:

 /**
* 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) {
heap = vector<ListNode*>(lists.size(), );
sz = ;
for(size_t i = ; i < lists.size(); ++i)
if(lists[i]) push(lists[i]);
ListNode *head = NULL, *p = head;
while(sz) {
if(head == NULL)
p = head = pop();
else {
p->next = pop();
p = p->next;
}
if(p->next) push(p->next);
}
return head;
}
void push(ListNode *p) {
int child = sz++;
while(child > ) {
int father = (child-) / ;
if(p->val >= heap[father]->val) break;
heap[child] = heap[father];
child = father;
}
heap[child] = p;
}
ListNode* pop() {
ListNode *pAns = heap[];
heap[] = heap[--sz];
int father = , child = ;
ListNode *p = heap[father];
while(child < sz) {
if(child+ < sz && heap[child]->val > heap[child+]->val) ++child;
if(heap[child]->val >= p->val) break;
heap[father] = heap[child];
father = child;
child = * father + ;
}
heap[father] = p;
return pAns;
}
private:
int sz;
vector<ListNode*> heap;
};

优化后(增强易读性):

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Heap {
public:
Heap(size_t n) : sz(0), heap(vector<ListNode*>(n, NULL)) {}
void push(ListNode *p);
ListNode* pop();
int size() { return sz; }
private:
int sz;
vector<ListNode*> heap;
};
inline void Heap::push(ListNode *p) {
int child = sz++;
while(child > 0) {
int father = (child-1) / 2;
if(p->val >= heap[father]->val) break;
heap[child] = heap[father];
child = father;
}
heap[child] = p;
}
inline ListNode* Heap::pop() {
ListNode *pAns = heap[0];
heap[0] = heap[--sz];
int father = 0, child = 1;
ListNode *p = heap[father];
while(child < sz) {
if(child+1 < sz && heap[child]->val > heap[child+1]->val) ++child;
if(heap[child]->val >= p->val) break;
heap[father] = heap[child];
father = child;
child = 2 * father + 1;
}
heap[father] = p;
return pAns;
}
class Solution {
public:
ListNode *mergeKLists(vector<ListNode *> &lists) {
Heap heap(lists.size());
for(size_t i = 0; i < lists.size(); ++i)
if(lists[i]) heap.push(lists[i]);
ListNode *head = NULL, *p = head;
while(heap.size()) {
if(head == NULL)
p = head = heap.pop();
else {
p->next = heap.pop();
p = p->next;
}
if(p->next) heap.push(p->next);
}
return head;
}
};

71. Merge k Sorted Lists的更多相关文章

  1. Merge k Sorted Lists

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

  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练习题】Merge k Sorted Lists

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

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

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

  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-algorithms-23 Merge k Sorted Lists

    leetcode-algorithms-23 Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted ...

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

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

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

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

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

随机推荐

  1. C#编程语言与面向对象——类与对象

    由于ASP.NET技术是全面向对象的,因此,要掌握这一技术,必须具备有扎实的面向对象理论基础 使用C#编程,所有的程序代码几乎都放在类中,不存在独立于类中之外的函数,因此,类是面向对象编程的基本单元 ...

  2. ABAP屏幕设计

    转自 http://www.cnblogs.com/aBaoRong/archive/2012/06/05/2536591.html abap 屏幕控制 ******************** 屏幕 ...

  3. C# 获取 oracle 存储过程的 返回值

    存储过程 CREATE OR REPLACE PROCEDURE ADMIN.INSERT_OBJ ( OBJEFIRT_parms IN NVARCHAR2, OBJEDATT_parms IN N ...

  4. 为windows应用程序提供托盘图标

    1>包含头文件 #include "Shellapi.h"   2>相关结构体和函数:     NOTIFYICONDATA     WINSHELLAPI BOOL ...

  5. 跟我学Windows Azure 五 使用Cloub Service连接Blob Service完成图片的上传

    首先,我们创建一个云服务项目,用来演示我们的blob存储 下来我们修改我们我们云服务的名字 我们需要添加一个空的WebForm的项目 点击完成,我们可以看到我们的解决方案已经添加完成 下来我们需要添加 ...

  6. 【耐克】【Air Max90 气垫跑鞋】

    [max90 36-44] [加毛冬款 36-44] [黑白百搭款 36-44] [air max90 高帮 冬款 耐看百搭 36-44] [air max90 高帮 40-44] [Air Max9 ...

  7. python property详解

    Python中有一个被称为属性函数(property)的小概念,它可以做一些有用的事情.在这篇文章中,我们将看到如何能做以下几点: 将类方法转换为只读属性 重新实现一个属性的setter和getter ...

  8. 有关vue的总结

    1:使用v-for进行循环渲染: <div v-for="(value, key, index) in object"> {{ index }}. {{ key }} ...

  9. VS中Debug和Realease、及静态库和动态库的区别整理(转)

    原文出自:http://www.cnblogs.com/chensu/p/5632486.html 一.Debug和Realease区别产生的原因 Debug 通常称为调试版本,它包含调试信息,并且不 ...

  10. 【MVC】ASP.NET MVC Forms验证机制

    http://www.cnblogs.com/bomo/p/3309766.html 随笔 - 121  文章 - 0  评论 - 92 [MVC]ASP.NET MVC Forms验证机制 ASP. ...