mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 12:20:57 +00:00
Review Swift codes (#1150)
* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* File: heap.swift
|
||||
* Created Time: 2024-03-17
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import HeapModule
|
||||
import utils
|
||||
|
||||
func testPush(heap: inout Heap<Int>, val: Int) {
|
||||
heap.insert(val)
|
||||
print("\n元素 \(val) 入堆后\n")
|
||||
PrintUtil.printHeap(queue: heap.unordered)
|
||||
}
|
||||
|
||||
func testPop(heap: inout Heap<Int>) {
|
||||
let val = heap.removeMax()
|
||||
print("\n堆顶元素 \(val) 出堆后\n")
|
||||
PrintUtil.printHeap(queue: heap.unordered)
|
||||
}
|
||||
|
||||
@main
|
||||
enum _Heap {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* 初始化堆 */
|
||||
// Swift 的 Heap 类型同时支持最大堆和最小堆
|
||||
var heap = Heap<Int>()
|
||||
|
||||
/* 元素入堆 */
|
||||
testPush(heap: &heap, val: 1)
|
||||
testPush(heap: &heap, val: 3)
|
||||
testPush(heap: &heap, val: 2)
|
||||
testPush(heap: &heap, val: 5)
|
||||
testPush(heap: &heap, val: 4)
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
let peek = heap.max()
|
||||
print("\n堆顶元素为 \(peek!)\n")
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
testPop(heap: &heap)
|
||||
testPop(heap: &heap)
|
||||
testPop(heap: &heap)
|
||||
testPop(heap: &heap)
|
||||
testPop(heap: &heap)
|
||||
|
||||
/* 获取堆大小 */
|
||||
let size = heap.count
|
||||
print("\n堆元素数量为 \(size)\n")
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
let isEmpty = heap.isEmpty
|
||||
print("\n堆是否为空 \(isEmpty)\n")
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
// 时间复杂度为 O(n) ,而非 O(nlogn)
|
||||
let heap2 = Heap([1, 3, 2, 5, 4])
|
||||
print("\n输入列表并建立堆后")
|
||||
PrintUtil.printHeap(queue: heap2.unordered)
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ class MaxHeap {
|
||||
// 将列表元素原封不动添加进堆
|
||||
maxHeap = nums
|
||||
// 堆化除叶节点以外的其他所有节点
|
||||
for i in stride(from: parent(i: size() - 1), through: 0, by: -1) {
|
||||
for i in (0 ... parent(i: size() - 1)).reversed() {
|
||||
siftDown(i: i)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ func topKHeap(nums: [Int], k: Int) -> [Int] {
|
||||
// 初始化一个小顶堆,并将前 k 个元素建堆
|
||||
var heap = Heap(nums.prefix(k))
|
||||
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||
for i in stride(from: k, to: nums.count, by: 1) {
|
||||
for i in nums.indices.dropFirst(k) {
|
||||
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||
if nums[i] > heap.min()! {
|
||||
_ = heap.removeMin()
|
||||
|
||||
Reference in New Issue
Block a user