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个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...
随机推荐
- ExtJS4.2学习(三)Grid表格(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-07/172.html --------------- ...
- jquery判断对象是否获得焦点
var isFocus=$("#tRow").is(":focus"); if(true==isFocus){ alert("focus") ...
- 破解之API断点法
上回给大家做的破解教程,地址是http://www.52pojie.net/thread-52719-1-1.html,用的是“调用堆栈”方法.今天给新手提供另一种方法“API函数断点”,这种方法要求 ...
- Python性能鸡汤
http://pythoner.org/wiki/257/ 毫无疑问:Python程序没有编译型语言高效快速. 甚至Python拥护者们会告诉你Python不适合这些领域. 然而,YouTube已用P ...
- android 使用Activity做窗口弹出(模拟Dialog)
我们下面使用Activity,模拟一个dialog: 首先看布局: <?xml version="1.0" encoding="utf-8"?> & ...
- android 点击重新加载界面设计
在项目中经常会遇到这样的场合,用户点击了一个界面后要提示等待加载,最后有可能显示加载失败,点击屏幕再重试加载.下面是该实例的代码: layout: loading.xml <?xml versi ...
- 【转】wireshark过滤规则
WireShark过滤语法 1.过滤IP,如来源IP或者目标IP等于某个IP 例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.add ...
- Qt之QtSoap(访问WebService)
http://blog.csdn.net/u011012932/article/details/51673800
- Android 获取TextView 显示的字符串宽度
工作上有业务需要判断textview是否换行,我的做法是判断textview要显示的字符串的宽度是否超过我设定的宽度,若超过则会执行换行. 项目中的其他地方也有这样的需求,故直接使用了那一块的代码.如 ...
- Android安装常见问题
在初次运行Android程序的时候会出现类似的错误,导致程序无法继续运行,如下面的几个例子: 问题1:PC安卓模拟器 PANIC: Could not open: C:\Documents and S ...