当前位置:网站首页>算法--合并K个升序链表(Kotlin)
算法--合并K个升序链表(Kotlin)
2022-04-21 22:13:00 【小米科技Android 研发曹新雨】
题目:
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:
输入:lists = []
输出:[]
示例 3:
输入:lists = [[]]
输出:[]
提示:
k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决方法:
1.使用优先队列合并
fun mergeKLists(lists: Array<ListNode?>): ListNode? {
var priorityQueue = PriorityQueue<ListNode> { o1, o2 -> if (o1.`val` >= o2.`val`) 1 else -1 }
for (list:ListNode? in lists) {
if (list != null) {
priorityQueue.add(list)
}
}
var dump = ListNode(-1)
var cur = dump
while (!priorityQueue.isEmpty()){
val poll = priorityQueue.poll()
if (poll.next != null) {
priorityQueue.add(poll.next)
}
cur.next = poll
cur = cur.next
}
return dump.next
}
2.顺序合并
fun mergeTwoLists3(l1: ListNode?, l2: ListNode?): ListNode? {
return if (l1 == null){
l2
}else if (l2 == null){
l1
}else if (l1.`val` > l2.`val`){
l2.next = mergeTwoLists3(l1,l2.next)
l2
}else{
l1.next = mergeTwoLists3(l1.next,l2)
l1
}
}
fun mergeKLists(lists: Array<ListNode?>): ListNode? {
var result :ListNode? = null
for (list in lists) {
result = mergeTwoLists3(result,list)
}
return result
}
3.使用分治方法
fun mergeKLists(lists: Array<ListNode?>): ListNode? {
return mergeKMid(lists,0,lists.size-1)
}
fun mergeTwoLists3(l1: ListNode?, l2: ListNode?): ListNode? {
return if (l1 == null){
l2
}else if (l2 == null){
l1
}else if (l1.`val` > l2.`val`){
l2.next = mergeTwoLists3(l1,l2.next)
l2
}else{
l1.next = mergeTwoLists3(l1.next,l2)
l1
}
}
fun mergeKMid(lists: Array<ListNode?>,left:Int,right:Int): ListNode? {
if (left == right){
return lists[left]
}
if(left > right){
return null
}
var mid = (right + left) / 2
return mergeTwoLists3(mergeKMid(lists,left,mid), mergeKMid(lists,mid+1,right))
}
版权声明
本文为[小米科技Android 研发曹新雨]所创,转载请带上原文链接,感谢
https://caoxinyu.blog.csdn.net/article/details/124329574
边栏推荐
- 软件测试分类与软件测试的原则
- Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name
- Classification of software testing and principles of software testing
- STM32外设GPIO的配置和应用
- [ES6] function extension
- php如何给数组增加一个数组元素
- 2022年重庆最新建筑八大员(土建)模拟题库及答案
- Software life cycle
- Test cases and design methods
- WPF data-driven method for modifying binding
猜你喜欢

智慧化工园区解决方案

Huayun actively responded to the anti epidemic call of Hefei high tech Zone: practicing social responsibility and contributing to the strength of science and technology enterprises

Do you have any good suggestions for brushing questions and how to improve efficiency?
![[basic problems of function implementation C language]](/img/73/a400b286819f37a353cc6229e483fe.jpg)
[basic problems of function implementation C language]

「运维有小邓」企业如何解决AD域弱密码问题?

Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name

Outsourcing student management system architecture design document

IDEA通过Jedis操作Linux上的Redis;Failed to connect to any host resolved for DNS name问题

Kotlin crawler app, Android development, interview preparation

WPF data-driven method for modifying binding
随机推荐
[vscode] detailed user manual for debugger
Kotlin core programming, Android development, interview answer handler
2022年重庆最新建筑八大员(土建)模拟题库及答案
Mypinpad and smartpesa merged to become the global leader in mobile payment acceptance
ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice
[ES6] iterator and forof loop
【函数实现c语言基础问题】
字节流写输入
解放双手,推荐一款阿里开源的低代码工具,YYDS~
[ES6] array expansion
regular expression
Leetcode0785. 判断二分图(medium,二分图,DFS)
【Canvas】基础绘制与使用
Attack and defense world MFW
Database design and Implementation
GD32F103学习笔记(8)——ADC接口使用
[ES6] function extension
How does PHP add an array element to an array
es6如何求两个数组的交集
Windowns 离线安装WSL2