LeetCode 75. 颜色分类 75. 颜色分类解题思路 统计频次: 使用数组 count 分别统计 0、1、2 出现的次数 重写数组:根据统计结果,按顺序(0、1、2)将元素写回原数组 nums 参考代码12345678910111213141516class Solution { public void sortColors(int[] nums) { int[] count = new int[3]; for(int num : nums) { count[num] ++; } int startIndex = 0; for(int i = 0; i < count.length; i ++) { for(int j = 0; j < count[i]; j ++) { nums[startIndex ++] = i; } } }} LeetCode #技巧 LeetCode 75. 颜色分类 https://sowink.cn/2026/02/08/LeetCode-75-颜色分类/ 作者 Xurx 发布于 2026年2月8日 许可协议 LeetCode 74. 搜索二维矩阵 上一篇 LeetCode 76. 最小覆盖子串 下一篇 Please enable JavaScript to view the comments