This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View File

@ -305,6 +305,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{__init__}
```
=== "Zig"
```zig title="my_heap.zig"

View File

@ -378,6 +378,43 @@ comments: true
=== "Kotlin"
```kotlin title="heap.kt"
/* 初始化堆 */
// 初始化小顶堆
var minHeap = PriorityQueue<Int>()
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
/* 元素入堆 */
maxHeap.offer(1)
maxHeap.offer(3)
maxHeap.offer(2)
maxHeap.offer(5)
maxHeap.offer(4)
/* 获取堆顶元素 */
var peek = maxHeap.peek() // 5
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
peek = maxHeap.poll() // 5
peek = maxHeap.poll() // 4
peek = maxHeap.poll() // 3
peek = maxHeap.poll() // 2
peek = maxHeap.poll() // 1
/* 获取堆大小 */
val size = maxHeap.size
/* 判断堆是否为空 */
val isEmpty = maxHeap.isEmpty()
/* 输入列表并建堆 */
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
```
=== "Ruby"
```ruby title="heap.rb"
```
@ -636,6 +673,16 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{left}
[class]{MaxHeap}-[func]{right}
[class]{MaxHeap}-[func]{parent}
```
=== "Zig"
```zig title="my_heap.zig"
@ -767,6 +814,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{peek}
```
=== "Zig"
```zig title="my_heap.zig"
@ -1155,6 +1208,14 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{push}
[class]{MaxHeap}-[func]{sift_up}
```
=== "Zig"
```zig title="my_heap.zig"
@ -1703,6 +1764,14 @@ comments: true
}
```
=== "Ruby"
```ruby title="my_heap.rb"
[class]{MaxHeap}-[func]{pop}
[class]{MaxHeap}-[func]{sift_down}
```
=== "Zig"
```zig title="my_heap.zig"

View File

@ -434,6 +434,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="top_k.rb"
[class]{}-[func]{top_k_heap}
```
=== "Zig"
```zig title="top_k.zig"