Skip to content

Commit b924394

Browse files
committed
二刷75
1 parent 3073341 commit b924394

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

docs/0000-counting-sort.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33

44

55
image::images/counting-sort-overview.png[{image_attr}]
6+
7+
== 经典题目
8+
9+
. xref:0075-sort-colors.adoc[75. Sort Colors]

docs/0000-quick-sort.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ image::images/quick-sort-01.gif[{image_attr}]
1515
image::images/quick-sort-02.gif[{image_attr}]
1616

1717
image::images/quick-sort-03.png[{image_attr}]
18+
19+
20+
NOTE: 研究一下三路快排。

docs/0075-sort-colors.adoc

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
11
[#0075-sort-colors]
2-
= 75. Sort Colors
2+
= 75. 颜色分类
33

4-
{leetcode}/problems/sort-colors/[LeetCode - Sort Colors^]
4+
https://leetcode.cn/problems/sort-colors/[LeetCode - 75. 颜色分类^]
55

6-
Given an array with _n_ objects colored red, white or blue, sort them *https://en.wikipedia.org/wiki/In-place_algorithm[in-place^] *so that objects of the same color are adjacent, with the colors in the order red, white and blue.
6+
给定一个包含红色、白色和蓝色、共 `n` 个元素的数组 `nums`**https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95[原地]**对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
77

8-
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
8+
我们使用整数 `0``1``2` 分别表示红色、白色和蓝色。
99

10-
*Note:* You are not suppose to use the library's sort function for this problem.
10+
必须在不使用库内置的 sort 函数的情况下解决这个问题。
1111

12-
*Example:*
12+
*示例 1:*
1313

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [2,0,2,1,1,0]
17-
*Output:* [0,0,1,1,2,2]
18-
----
14+
....
15+
输入:nums = [2,0,2,1,1,0]
16+
输出:[0,0,1,1,2,2]
17+
....
18+
19+
*示例 2:*
20+
21+
....
22+
输入:nums = [2,0,1]
23+
输出:[0,1,2]
24+
....
25+
26+
*提示:*
27+
28+
* `+n == nums.length+`
29+
* `1 \<= n \<= 300`
30+
* `nums[i]``0``1``2`
1931
20-
*Follow up:*
32+
*进阶:*
2133

34+
* 你能想出一个仅使用常数空间的一趟扫描算法吗?
2235
23-
* A rather straight forward solution is a two-pass algorithm using counting sort.
2436
2537
26-
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
27-
* Could you come up with a one-pass algorithm using only constant space?
38+
== 思路分析
2839

40+
经典的荷兰国旗问题,最合适的解法是:“三路快排”。
2941

42+
统计 `0`、 `1` 和 `2` 的个数。然后在数组中依次填入。
3043

3144
[[src-0075]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
3250
[{java_src_attr}]
3351
----
3452
include::{sourcedir}/_0075_SortColors.java[tag=answer]
3553
----
54+
--
55+
56+
二刷::
57+
+
58+
--
59+
[{java_src_attr}]
60+
----
61+
include::{sourcedir}/_0075_SortColors_2.java[tag=answer]
62+
----
63+
--
64+
====
65+
66+
67+
== 参考资料
3668

69+
. https://leetcode.cn/problems/sort-colors/solutions/437968/yan-se-fen-lei-by-leetcode-solution/[75. 颜色分类 - 官方题解^]
70+
. https://leetcode.cn/problems/sort-colors/solutions/3679069/on-cha-ru-pai-xu-jian-ji-xie-fa-pythonja-zk60/[75. 颜色分类 - O(n) 插入排序,简洁写法^]
71+
. https://leetcode.cn/problems/sort-colors/solutions/71322/kuai-su-pai-xu-partition-guo-cheng-she-ji-xun-huan/[75. 颜色分类 - 「三路快排」应用^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,11 @@ endif::[]
19341934
|{doc_base_url}/0076-minimum-window-substring.adoc[题解]
19351935
|✅ 滑动窗口。字母不够时,扩大窗口;字母足够时,开始缩小窗口。
19361936

1937+
|{counter:codes2503}
1938+
|{leetcode_base_url}/sort-colors/[75. Sort Colors^]
1939+
|{doc_base_url}/0075-sort-colors.adoc[题解]
1940+
|✅ 计数排序或三路快排。
1941+
19371942
|===
19381943

19391944
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0075_SortColors_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-11-17 21:37:31
8+
*/
9+
public void sortColors(int[] nums) {
10+
int[] counter = new int[3];
11+
for (int num : nums) {
12+
counter[num]++;
13+
}
14+
int index = 0;
15+
for (int i = 0; i < counter.length; i++) {
16+
for (int j = 0; j < counter[i]; j++) {
17+
nums[index++] = i;
18+
}
19+
}
20+
}
21+
// end::answer[]
22+
}

0 commit comments

Comments
 (0)