This commit is contained in:
krahets
2023-08-13 19:36:03 +08:00
parent 059a849167
commit 971624c291
31 changed files with 1750 additions and 156 deletions
+2 -2
View File
@@ -276,7 +276,7 @@ comments: true
for (int num in nums) {
m = max(m, num);
}
// 2. 统计各数字的出现次数
// 2. 统计各数字的出现次数
// counter[num] 代表 num 的出现次数
List<int> counter = List.filled(m + 1, 0);
for (int num in nums) {
@@ -728,7 +728,7 @@ $$
for (int i = n - 1; i >= 0; i--) {
int num = nums[i];
res[counter[num] - 1] = num; // 将 num 放置到对应索引处
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
counter[num]--; // 令前缀和自减 1 ,得到下次放置 num 的索引
}
// 使用结果数组 res 覆盖原数组 nums
nums.setAll(0, res);