当前位置: 首页 > news >正文

23. Merge k Sorted Lists

目录

题目描述

方法一、k-1次两两合并

方法二、分治法合并

方法三、使用优先队列


题目描述

23. Merge k Sorted Lists

方法一、k-1次两两合并

选第一个链表作为结果链表,每次将后面未合并的链表合并到结果链表中,经过k-1次合并,即可得到答案。假设每个链表的最长长度是n,时间复杂度O(n+2n+3n+...(k-1)n) = O(\frac{k(k-1))}{2}n) = O(k^{2}n)。空间复杂度O(1)。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* mergeKLists(vector<ListNode*>& lists) {int n = lists.size();if(n == 0)return nullptr;ListNode* ans = lists[0];for(int i = 1;i< n;i++){ans = merge(ans,lists[i]);}return ans;}ListNode* merge(ListNode* L1,ListNode* L2){ListNode* dummy = new ListNode();ListNode* cur = dummy;while(L1&&L2){if(L1->val < L2->val){cur->next = L1;cur = L1;L1 = L1->next;}else{cur->next = L2;cur = L2;L2 = L2->next;}}cur->next = L1 != nullptr ? L1 : L2;ListNode* res = dummy->next;delete dummy;return res;}
};

方法二、分治法合并

时间复杂度 O(kn×logk)。空间复杂度 O(logk) 。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* mergeKLists(vector<ListNode*>& lists) {int n = lists.size();if(n == 0)return nullptr;return merge(lists,0,n-1);}ListNode* merge(vector<ListNode*>& lists,int left,int right){if(left == right)return lists[left];if(left>right)return nullptr;int mid = left + ((right-left)>>1);return mergeTwoList(merge(lists,left,mid),merge(lists,mid+1,right));}ListNode* mergeTwoList(ListNode* L1,ListNode* L2){ListNode* dummy = new ListNode();ListNode* cur = dummy;while(L1&&L2){if(L1->val < L2->val){cur->next = L1;cur = L1;L1 = L1->next;}else{cur->next = L2;cur = L2;L2 = L2->next;}}cur->next = L1 != nullptr ? L1 : L2;ListNode* res = dummy->next;delete dummy;return res;}
};

方法三、使用优先队列

时间复杂度 O(kn×logk)。空间复杂度 O(k) 。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {struct Node{ListNode* node_ptr;int val;bool operator<(const Node& rhs) const{return val>rhs.val;}};
public:ListNode* mergeKLists(vector<ListNode*>& lists) {priority_queue<Node> Heap;for(auto& node:lists){if(node){Heap.push({node,node->val});}}ListNode* head = nullptr;ListNode* cur = nullptr;while(!Heap.empty()){if(head == nullptr){head = Heap.top().node_ptr;cur = head;}else{cur->next = Heap.top().node_ptr;cur = cur->next;}Heap.pop();if(cur->next){Heap.push({cur->next,cur->next->val});}}return head;}
};
http://www.lqws.cn/news/81199.html

相关文章:

  • #16 学习日志软件测试
  • 并查集(上)
  • DAY 40 超大力王爱学Python
  • 【多线程初阶】内存可见性问题 volatile
  • Java线程生命周期详解
  • Promise与Async/Await:现代JavaScript异步编程的利器
  • 高效使用Map的“新”方法
  • 模块二:C++核心能力进阶(5篇)篇二:《多线程编程:C++线程池与原子操作实战》(14万字深度指南)
  • openai-java
  • Java详解LeetCode 热题 100(23):LeetCode 206. 反转链表(Reverse Linked List)详解
  • 做好 4个基本动作,拦住性能优化改坏原功能的bug
  • ps色彩平衡调整
  • github 提交失败,连接不上
  • 数值与字典解决方案二十七讲:两列数据相互去掉重复值后合并
  • 【Java Web】速通Tomcat
  • 【性能调优系列】深入解析火焰图:从基础阅读到性能优化实战
  • 导入典籍数据
  • Docker 镜像原理
  • React 核心概念与生态系统
  • js的时间循环的讲解
  • sqlite-vec:谁说SQLite不是向量数据库?
  • 题目 3225: 蓝桥杯2024年第十五届省赛真题-回文字符串
  • 光伏功率预测 | LSTM多变量单步光伏功率预测(Matlab完整源码和数据)
  • 机器视觉图像处理之图像滤波
  • 从多巴胺的诱惑到内啡肽的力量 | 个体成长代际教育的成瘾困局与破局之道
  • Python----目标检测(《YOLO9000: Better, Faster, Stronger》和YOLO-V2的原理与网络结构)
  • 蓝云APP:云端存储,便捷管理
  • Linux入门(十三)动态监控系统监控网络状态
  • (Python网络爬虫);抓取B站404页面小漫画
  • 探秘 Minimax:AI 领域的创新先锋