This commit is contained in:
krahets
2024-03-21 04:22:07 +08:00
parent 35a07170c0
commit cfdb743939
52 changed files with 292 additions and 290 deletions
+1 -1
View File
@@ -306,7 +306,7 @@ comments: true
siftDown(nums: &nums, n: nums.count, i: i)
}
// 从堆中提取最大元素,循环 n-1 轮
for i in stride(from: nums.count - 1, to: 0, by: -1) {
for i in nums.indices.dropFirst().reversed() {
// 交换根节点与最右叶节点(交换首元素与尾元素)
nums.swapAt(0, i)
// 以根节点为起点,从顶至底进行堆化