This commit is contained in:
krahets
2024-05-15 19:00:27 +08:00
parent bd54cd096b
commit e434a3343c
36 changed files with 402 additions and 107 deletions
+3 -3
View File
@@ -314,7 +314,7 @@ comments: true
let k = nums.len() / 2;
let mut buckets = vec![vec![]; k];
// 1. 将数组元素分配到各个桶中
for &mut num in &mut *nums {
for &num in nums.iter() {
// 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
let i = (num * k as f64) as usize;
// 将 num 添加进桶 i
@@ -327,8 +327,8 @@ comments: true
}
// 3. 遍历桶合并结果
let mut i = 0;
for bucket in &mut buckets {
for &mut num in bucket {
for bucket in buckets.iter() {
for &num in bucket.iter() {
nums[i] = num;
i += 1;
}