【Lintcode】104.Merge k Sorted Lists
题目:
Merge k sorted linked lists and return it as one sorted list.
Analyze and describe its complexity.
Example
Given lists:
[
2->4->null,
null,
-1->null
],
return -1->2->4->null
.
题解:
Solution 1 ()
class Solution {
public:
struct compare {
bool operator() (const ListNode* a, const ListNode* b) {
return a->val > b->val;
}
};
ListNode* mergeKLists(vector<ListNode *> &lists) {
priority_queue<ListNode*, vector<ListNode*>, compare> q;
for (auto l : lists) {
if (l) {
q.push(l);
}
}
ListNode* head = nullptr, *pre = nullptr, *tmp = nullptr;
while (!q.empty()) {
tmp = q.top();
q.pop();
if(!pre) {
head = tmp;
} else {
pre->next = tmp;
}
pre = tmp;
if (tmp->next) {
q.push(tmp->next);
}
} return head;
}
};
Solution 2 ()
class Solution {
public:
ListNode* mergeKLists(vector<ListNode *> &lists) {
if (lists.empty()) {
return nullptr;
}
int n = lists.size();
while (n > ) {
int k = (n + ) / ;
for (int i = ; i < n / ; ++i) {
lists[i] = mergeTwoLists(lists[i], lists[i + k]);
}
n = k;
}
return lists[];
} ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(-);
ListNode* cur = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) {
cur->next = l1;
} else {
cur->next = l2;
} return dummy->next;
}
};
Solution 3 ()
class Solution {
public:
static bool heapComp(ListNode* a, ListNode* b) {
return a->val > b->val;
}
ListNode* mergeKLists(vector<ListNode*>& lists) { //make_heap
ListNode head();
ListNode *curNode = &head;
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 head.next;
}
};
【Lintcode】104.Merge k Sorted Lists的更多相关文章
- 【原创】leetCodeOj --- Merge k Sorted Lists 解题报告
题目地址: https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题目内容: /** * Definition for singly-linke ...
- 【LeetCode】023. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【LeetCode】23. Merge k Sorted Lists
合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【一天一道LeetCode】#23. Merge k Sorted Lists
一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【LeetCode】21. Merge Two Sorted Lists
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- 【leetcode】 21. Merge Two Sorted Lists
题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...
- 【LeetCode】021. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- Lua学习五----------Lua循环
© 版权声明:本文为博主原创文章,转载请注明出处 1.循环类型 1.1 while循环 - 语法:while(condition) do ...<执行语句> end - 解析:判断cond ...
- wpf SplitButton
SplitButton该控件除了本身Button 的功能外,还具有下拉菜单的功能,能够在按键右側加入下拉菜单控件: <SplitButton Content="..." ...
- 一、Silverlight中使用MVVM(一)——基础
如果你不知道MVVM模式,我建议你先了解一下MVVM模式,至少要知道实现该模式的意图是什么. 那么我主要通过我认为是已经算是比较简单的例子进行讲解这个模式,当然后面我们会在这个例子的基础上一步一步的进 ...
- c# .net 关于接口实现方式:隐式实现/显式实现!
以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,“显示接口实现”就是使用接口名称作为方法 ...
- jQuery入门知识点
<精通ASP.NET MVC3框架>第20章 1.jQuery文件jquery-1.5.1.js:jquey核心库常规版jquery-1.5.1.min.js:jquery核心库最小化版j ...
- 在dev目录创建一个字符设备驱动的流程
1.struct file_operations 字符设备文件接口 1: static int mpu_open(struct inode *inode, struct file *file) 2: ...
- 我的Android进阶之旅------>Android实现音乐示波器、均衡器、重低音和音场功能
本实例来自于<疯狂Android讲义>,要实现具体的功能,需要了解以下API: MediaPlayer 媒体播放器 Visualizer 频谱 Equalizer 均衡器 BassBoo ...
- Cannot run program “git.exe”: createprocess error=2,系统找不到指定的文件
Android Studio提供VCS(Version Control System)版本控制系统,默认情况使用Git.GitHub工具需要配置git.exe路径,否则提示“cannot run pr ...
- 查看物料凭证MB03 /MIGO A04-显示,R02-物料凭证
当货物移动操作后,可以使用事物码MB03.MIGO查询最近一次生成的物料凭证, 如果未知凭证号,Table:MKPF / AUFM/EKBE MKPF 抬头:物料凭证 KEY: MBLNR 物料凭证编 ...
- java基础之容器、集合、集合常用方法
一.容器(Collection):数组是一种容器,集合也是一种容器 java编程中,装其他各种各样的对象(引用类型)的一种东西,叫容器 注意: 1.数组的长度是固定的 2.集合:长度不固定, 可以随时 ...