[LeetCode 题解]: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
题意:对k个有序的链表进行归并排序。并分析其复杂度。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* merge(ListNode* left, ListNode* right){ //归并操作
if(left==NULL) return right;
if(right==NULL) return left;
ListNode *ans =new ListNode();
if(left->val <= right->val){
ans=left;
left = left->next;
}else{
ans=right;
right=right->next;
}
ans->next = merge(left,right);
return ans;
}
ListNode* mergesort(vector<ListNode *> &lists,int left,int right){ //devide && conquer
if(left>right){
return NULL;
}if(left==right){
return lists[left];
}else{
int middle = (left+right)>>;
ListNode *lleft = mergesort(lists,left,middle);
ListNode *lright = mergesort(lists,middle+,right);
return merge(lleft,lright);
} }
ListNode *mergeKLists(vector<ListNode *> &lists) {
int left =,right =lists.size()-;
int middle= (left+right)>>;
ListNode *lleft = mergesort(lists,left,middle);
ListNode *lright = mergesort(lists,middle+,right);
return merge(lleft,lright);
}
};
时间复杂度: O(n*logn)
空间复杂度: O(1)
至于具体的量化分析呢,呵呵。。。数学推导了
转载请注明出处: http://www.cnblogs.com/double-win/谢谢!
[LeetCode 题解]: Merge k Sorted Lists的更多相关文章
- 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 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 ...
- 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
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 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- leetcode 23. Merge k Sorted Lists(堆||分治法)
Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...
- [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 ...
随机推荐
- 关于Android的margin(当前视图与周围视图的距离)和padding(当前视图与内部内容的距离)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- RocketMQ入门(简介、特点)
简介: RocketMQ作为一款纯java.分布式.队列模型的开源消息中间件,支持事务消息.顺序消息.批量消息.定时消息.消息回溯等. 发展历程: 1. Metaq(Metamorphosis) 1. ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 18—Photo OCR 应用实例:图片文字识别
Lecture 18—Photo OCR 应用实例:图片文字识别 18.1 问题描述和流程图 Problem Description and Pipeline 图像文字识别需要如下步骤: 1.文字侦测 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 10—Advice for applying machine learning 机器学习应用建议
Lecture 10—Advice for applying machine learning 10.1 如何调试一个机器学习算法? 有多种方案: 1.获得更多训练数据:2.尝试更少特征:3.尝试更多 ...
- C# 对象封装为json格式
1 对象 public class Person { public string Name { get; set; } public int Age { get; set; } public Date ...
- springboot mvc beetl模板 自定义错误的后缀问题
@Component public class BeetlErrorViewResolver implements ErrorViewResolver { private static final M ...
- c# vs c++
[c# vs c++] 1.在 C++ 中,类和结构实际上是相同的,而在 C# 中,它们很不一样.C# 类可以实现任意数量的接口,但只能从一个基类继承.而且,C# Struct不支持继承,也不支持显式 ...
- JS遍历子孙树
function fn(dataList,parent_id){ var result = [] , temp; for(var i in dataList){ if( ...
- JAVA集合中的迭代器的遍历
JAVA中的迭代器,迭代实质上就是遍历,在JAVA中使用iterator()方法进行迭代.需要注意的是,iterator()方法的返回值是Iterator对象.Iterator对象有三个方法,hasN ...
- VMWare windows找不到microsoft软件许可条款
提示如下错误: windows找不到microsoft软件许可条款.请确保安装源有效,然后重新启动安装. 解决方案: 把该虚拟机中的系统硬件配置中的软盘去掉. 程序员的基础教程:菜鸟程序员