当前位置:网站首页>go优先级队列实现
go优先级队列实现
2022-08-07 14:05:00 【Johns】
背景
最近在leetcode刷题, 遇到一个合并K个升序链表的问题, 就是把K个有序链表合并成一个有序链表
想到了一个idea就是把K个链表的头放到一个优先级队列, 每次弹最小的出来, 并把弹出来的节点的Next节点push回队列, 不断循环就能解决这个问题, 奈何go没有像Java一样提供那么易用的API, 我们只能使用container/heap实现一个简单的优先级队列.
实现
package main
import (
"container/heap"
"fmt"
)
type ListNode struct {
Val int
Next *ListNode
}
// 定义一个优先级队列
type PriorityQueue []*ListNode
func (p PriorityQueue) Len() int {
return len(p)
}
// 小于(<)就是小顶堆, 大于(>)就是大顶堆
func (p PriorityQueue) Less(i, j int) bool {
return p[i].Val < p[j].Val
}
func (p PriorityQueue) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
return
}
func (p *PriorityQueue) Push(x interface{}) {
*p = append(*p, x.(*ListNode))
}
func (p *PriorityQueue) Pop() interface{} {
old := *p
n := len(old)
x := old[n-1]
*p = old[0 : n-1]
return x
}
func main() {
p := &PriorityQueue{}
heap.Init(p)
node1 := &ListNode{Val: 4}
node2 := &ListNode{Val: 3}
node3 := &ListNode{Val: 2}
node4 := &ListNode{Val: 6}
node5 := &ListNode{Val: 1}
heap.Push(p, node1)
heap.Push(p, node2)
heap.Push(p, node3)
heap.Push(p, node4)
heap.Push(p, node5)
for p.Len() > 0 {
node := heap.Pop(p)
fmt.Print(node.(*ListNode).Val, " ")
}
}输出:
1 2 3 4 6合并K个有序链表
有了上面定义好的优先级队列, 我们可以开始解决问题了
func mergeKLists(lists []*ListNode) *ListNode {
if len(lists)==0{
return nil
}
dummy := &ListNode{Val: -1}
p1 := dummy
// 使用一个优先级队列(小根堆), 每次把堆顶元素(最小值)弹出来
p := &PriorityQueue{}
heap.Init(p)
for i := 0; i < len(lists); i++ {
if lists[i]!=nil{
heap.Push(p, lists[i])
}
}
for p.Len() > 0 {
v := heap.Pop(p).(*ListNode)
p1.Next = v
if v.Next!=nil{
heap.Push(p, v.Next)
}
p1 = p1.Next
}
return dummy.Next
}
// 测试
func main() {
node1 := &ListNode{Val: 3}
node2 := &ListNode{Val: 4}
node1.Next = node2
node3 := &ListNode{Val: 1}
node4 := &ListNode{Val: 2}
node3.Next = node4
node5 := &ListNode{Val: 6}
node4.Next = node5
v := mergeKLists([]*ListNode{node1, node3})
fmt.Println(v)
}边栏推荐
猜你喜欢

ENScanGo主域名批量提取脚本

一种自主学习 Office Open XML 文件格式的方法介绍

Domain objects share data

CSO face to face | Dialogue with mini world, talk about the safety construction of the game industry

SwiftUI任意继承层级中视图被裁剪显示不全的解决方案

2022年危险化学品生产单位安全生产管理人员考试题模拟考试题库及答案
![[Leetcode]21. 合并两个有序链表](/img/86/e3a14a64dafece194e44aec91cea44.png)
[Leetcode]21. 合并两个有序链表

【YOLOv5】结合GradCAM热力图可视化

Realize the Circle Fill effect of Sprite and solve the problems in the atlas

传统网站与数字化网站的对比
随机推荐
HJ6 prime factor
开发者成长激励计划-开发笔记:最简步骤移植LVGL
下一代无线局域网-高吞吐率
移相带来的振荡器
室内定位之CSI指纹定位
Error: required args <xml=string> at error (index.esm.js?93ce:68:1) at Parser.parse (index.e
Qt implementation based on matchtemplate long shots
Redis docker 主从模式与哨兵sentinel
excel sumifs多条件求和
【MySql进阶】索引详解(二):Mysql InnoDB索引原理、B+树、聚簇索引、二级索引、联合索引
8. cuBLAS Development Guide Chinese version--cublasGetMatrix() and cublasSetMatrix() in cuBLAS
Developer Growth Incentive Program - Development Notes: The Simplest Steps to Transplant LVGL
HJ6 质数因子
Fiddler抓包原理讲解以及实例操作
postgresql逻辑备份工具pg_dump和pg_resotre学习
LOGO 8.3 Web Server功能
SQL教程之 掌握 SQL GROUP BY 的 5 个实用 SQL 示例(含完整sql与测试数据)
HJ8 合并表记录
图像特效显示(下)
HJ3 obvious random number