Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ struct helper {
ListNode *head;
int len;
helper(ListNode *h, int l) : head ( h ), len ( l ) {}
}; class helpercmp {
public:
bool operator() (const helper &a, const helper &b) {
return a.len > b.len;
}
}; class Solution {
public:
priority_queue<helper, vector<helper>, helpercmp> heap; inline int listSize(ListNode *head) {
int len = ;
for ( ; head != nullptr; head = head->next, len++ );
return len;
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
if ( lists.empty() ) return nullptr;
if ( lists.size() == ) return lists[];
ListNode *head = nullptr, *left = nullptr, *right = nullptr;
for ( auto list : lists ) {
heap.push( helper(list, listSize(list) ) );
}
while ( heap.size() != ) {
left = heap.top().head;
heap.pop();
right = heap.top().head;
heap.pop();
head = mergeList( left, right );
heap.push( helper( head, listSize(head) ) );
}
return heap.top().head;
}
ListNode *mergeList(ListNode *a, ListNode *b) {
ListNode dummy();
ListNode *tail = &dummy;
while ( a != nullptr && b != nullptr ) {
if ( a-> val <= b->val ) {
tail->next = a;
a = a->next;
} else {
tail->next = b;
b = b->next;
}
tail = tail->next;
}
tail->next = a == nullptr ? b : a;
return dummy.next;
}
};
Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution的更多相关文章
- 蜗牛慢慢爬 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 ...
- Java for LeetCode 023 Merge k Sorted Lists
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 ...
- LeetCode 023 Merge k Sorted Lists
题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- [leetcode 23]Merge k Sorted Lists
1 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexi ...
- [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆
转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...
- leetcode 【 Merge k Sorted Lists 】python 实现
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- leetcode 23. Merge k Sorted Lists(堆||分治法)
Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...
随机推荐
- 18:字符串-char型字符串
1 什么是字符串? 字符串是以空字符(\)结尾的字符数组.空字符的assii码为:0, 空格的ascii码为322 \0的作用'\0'是一个空字符标志,它的ASSII码为0,C++有好多处理字符串的函 ...
- c++ 容器类
#include <iostream> #include <vector> #include <list> #include <map> using n ...
- [转载]C#中各种计时器
1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 S ...
- spoj 62
看了题解 自己好水 ...... #include <cstdio> #include <cstdlib> struct node { int x,y; }; node ...
- [Firefly引擎][学习笔记一][已完结]带用户验证的聊天室
原地址:http://bbs.9miao.com/thread-44571-1-1.html 前言:早在群里看到大鸡蛋分享他们团队的Firefly引擎,但一直没有时间去仔细看看,恰好最近需要开发一个棋 ...
- IsBadReadPtr|IsBadWritePtr调试崩溃
遇到一未找到必然出现条件的崩溃,不知道什么时候能触发崩溃,崩溃dump显示,试图访问了非法的内存或者写入了非法的内存 此时如下两个函数就比较有用了: BOOL WINAPI IsBadReadPtr( ...
- 【leetcode】Permutations II (middle)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- HDU1353+贪心
注意精度. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<al ...
- Maven打包时囊括本地依赖的jar包
在开发中,偶尔会遇到一个问题:某些比较冷门的包,maven服务器上没有,而我们又必须用,通常情况下会在项目中建立一个lib文件夹.将这些包copy进去并加入buildpath,开发就可以继续了,如下图 ...
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...