71. Merge k Sorted Lists
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的更多相关文章
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 【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 ...
- [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 ...
- 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 ...
- python 中的堆 (heapq 模块)应用:Merge K Sorted Lists
堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...
- 蜗牛慢慢爬 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 ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
随机推荐
- datatables增删改查的实现
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
- 什么是html技术
HTML(Hyper Text Mark-up Language )即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是为了能把存放在一台电脑 ...
- Apache常用配置项
301是永久重定向 302是临时重定向 编辑虚拟主机的配置文件需要用到apache的rewrite模块 所以要用要先检查apache是否安装了rewrite模块 # ./bin/apachectl - ...
- jquery的checkbox 全选和全不选
今天写了一个checkbox的全选和全不选的功能: var check_all=function(){ if(this.checked){ //alert($(".adv_check_num ...
- $.map和$.extend来复制数组(OA差旅)
$.map:对数组的每个元素调用定义的回调函数并返回包含结果的数组. 对数组用指定的方法. //将原数组中每个元素加 4 转换为一个新数组. //jQuery 代码: $.map( [0,1,2], ...
- Unity 相关经典博客资源总结(持续更新)
就作为一个记录吧,把平时看过的Unity相关的一些好的Blog记录并分享. 好的论坛: Unity官方脚本 点评:这个不用说了,最核心的内容,理解整个Unity引擎的方方面面,梳理结构. Unity ...
- Android学习九:屏幕自适应
android中不同手机分辨率适配问题 在项目开发的过程中,同一个布局对应不同的手机会显示出不同的效果.导致这个现象产生的原因是不同手机的分辨率不同.在android sdk提供的帮助文档中,我们可以 ...
- zeromq:c,c++,golang及nodejs使用
官网:www.zeromq.org 消息队列比较:http://www.cnblogs.com/charlesblc/p/6058799.html zeromq的一些观点:http://www.cnb ...
- 【深度学习】之Caffe的solver文件配置(转载自csdn)
原文: http://blog.csdn.net/czp0322/article/details/52161759 今天在做FCN实验的时候,发现solver.prototxt文件一直用的都是mode ...
- # C/C++的笔试题目
p,li { white-space: pre-wrap } # C/C++的笔试题目 + include<> 和include"" 的区别 + sizeof操作符与字 ...