feat: add Swift codes for top_k article (#578)
This commit is contained in:
parent
55d8b710fa
commit
b4de2c0d13
@ -39,6 +39,7 @@ let package = Package(
|
|||||||
.executable(name: "avl_tree", targets: ["avl_tree"]),
|
.executable(name: "avl_tree", targets: ["avl_tree"]),
|
||||||
// chapter_heap
|
// chapter_heap
|
||||||
.executable(name: "my_heap", targets: ["my_heap"]),
|
.executable(name: "my_heap", targets: ["my_heap"]),
|
||||||
|
.executable(name: "top_k", targets: ["top_k"]),
|
||||||
// chapter_graph
|
// chapter_graph
|
||||||
.executable(name: "graph_adjacency_matrix", targets: ["graph_adjacency_matrix"]),
|
.executable(name: "graph_adjacency_matrix", targets: ["graph_adjacency_matrix"]),
|
||||||
.executable(name: "graph_adjacency_list", targets: ["graph_adjacency_list"]),
|
.executable(name: "graph_adjacency_list", targets: ["graph_adjacency_list"]),
|
||||||
@ -107,6 +108,7 @@ let package = Package(
|
|||||||
.executableTarget(name: "avl_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["avl_tree.swift"]),
|
.executableTarget(name: "avl_tree", dependencies: ["utils"], path: "chapter_tree", sources: ["avl_tree.swift"]),
|
||||||
// chapter_heap
|
// chapter_heap
|
||||||
.executableTarget(name: "my_heap", dependencies: ["utils"], path: "chapter_heap", sources: ["my_heap.swift"]),
|
.executableTarget(name: "my_heap", dependencies: ["utils"], path: "chapter_heap", sources: ["my_heap.swift"]),
|
||||||
|
.executableTarget(name: "top_k", dependencies: ["utils"], path: "chapter_heap", sources: ["top_k.swift"]),
|
||||||
// chapter_graph
|
// chapter_graph
|
||||||
.executableTarget(name: "graph_adjacency_matrix", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_matrix.swift"]),
|
.executableTarget(name: "graph_adjacency_matrix", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_matrix.swift"]),
|
||||||
.executableTarget(name: "graph_adjacency_list", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list.swift"]),
|
.executableTarget(name: "graph_adjacency_list", dependencies: ["utils"], path: "chapter_graph", sources: ["graph_adjacency_list.swift"]),
|
||||||
|
35
codes/swift/chapter_heap/top_k.swift
Normal file
35
codes/swift/chapter_heap/top_k.swift
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* File: top_k.swift
|
||||||
|
* Created Time: 2023-07-02
|
||||||
|
* Author: nuomi1 (nuomi1@qq.com)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
|
/* 基于堆查找数组中最大的 k 个元素 */
|
||||||
|
func topKHeap(nums: [Int], k: Int) -> [Int] {
|
||||||
|
// 将数组的前 k 个元素入堆
|
||||||
|
var heap = Array(nums.prefix(k))
|
||||||
|
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||||
|
for i in stride(from: k, to: nums.count, by: 1) {
|
||||||
|
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||||
|
if nums[i] > heap.first! {
|
||||||
|
heap.removeFirst()
|
||||||
|
heap.insert(nums[i], at: 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return heap
|
||||||
|
}
|
||||||
|
|
||||||
|
@main
|
||||||
|
enum TopK {
|
||||||
|
/* Driver Code */
|
||||||
|
static func main() {
|
||||||
|
let nums = [1, 7, 6, 3, 2]
|
||||||
|
let k = 3
|
||||||
|
|
||||||
|
let res = topKHeap(nums: nums, k: k)
|
||||||
|
print("最大的 \(k) 个元素为")
|
||||||
|
PrintUtil.printHeap(queue: res)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user