22
33🟠 <font color =#ffb800 >Medium</font >&emsp ; 🔖&ensp ; [ ` 数组 ` ] ( /leetcode/outline/tag/array.md ) [ ` 双指针 ` ] ( /leetcode/outline/tag/two-pointers.md ) [ ` 排序 ` ] ( /leetcode/outline/tag/sorting.md ) &emsp ; 🔗&ensp ; [ ` LeetCode ` ] ( https://leetcode.com/problems/sort-colors )
44
5+
56## 题目
67
78Given an array ` nums ` with ` n ` objects colored red, white, or blue, sort them
@@ -14,58 +15,84 @@ and blue, respectively.
1415
1516You must solve this problem without using the library's sort function.
1617
18+
19+
1720** Example 1:**
1821
1922> Input: nums = [ 2,0,2,1,1,0]
20- >
23+ >
2124> Output: [ 0,0,1,1,2,2]
2225
2326** Example 2:**
2427
2528> Input: nums = [ 2,0,1]
26- >
29+ >
2730> Output: [ 0,1,2]
2831
2932** Constraints:**
3033
31- - ` n == nums.length `
32- - ` 1 <= n <= 300 `
33- - ` nums[i] ` is either ` 0 ` , ` 1 ` , or ` 2 ` .
34+ * ` n == nums.length `
35+ * ` 1 <= n <= 300 `
36+ * ` nums[i] ` is either ` 0 ` , ` 1 ` , or ` 2 ` .
37+
3438
35- ** Follow up:** Could you come up with a one-pass algorithm using only
39+
40+ ** Follow up:** Could you come up with a one-pass algorithm using only
3641constant extra space?
3742
43+
3844## 题目大意
3945
40- 给定一个包含红色、白色和蓝色、共 ` n ` 个元素的数组 ` nums `
46+ 给定一个包含红色、白色和蓝色、共 ` n ` __ 个元素的数组 ` nums `
4147,** [ 原地] ( https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95 )
4248** 对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
4349
4450我们使用整数 ` 0 ` 、 ` 1 ` 和 ` 2 ` 分别表示红色、白色和蓝色。
4551
4652必须在不使用库内置的 sort 函数的情况下解决这个问题。
4753
54+
55+
4856** 示例 1:**
4957
58+ >
59+ >
60+ >
61+ >
62+ >
5063> ** 输入:** nums = [ 2,0,2,1,1,0]
51- >
64+ >
5265> ** 输出:** [ 0,0,1,1,2,2]
66+ >
67+ >
5368
5469** 示例 2:**
5570
71+ >
72+ >
73+ >
74+ >
75+ >
5676> ** 输入:** nums = [ 2,0,1]
57- >
77+ >
5878> ** 输出:** [ 0,1,2]
79+ >
80+ >
81+
82+
5983
6084** 提示:**
6185
62- - ` n == nums.length `
63- - ` 1 <= n <= 300 `
64- - ` nums[i] ` 为 ` 0 ` 、` 1 ` 或 ` 2 `
86+ * ` n == nums.length `
87+ * ` 1 <= n <= 300 `
88+ * ` nums[i] ` 为 ` 0 ` 、` 1 ` 或 ` 2 `
89+
90+
6591
6692** 进阶:**
6793
68- - 你能想出一个仅使用常数空间的一趟扫描算法吗?
94+ * 你能想出一个仅使用常数空间的一趟扫描算法吗?
95+
6996
7097## 解题思路
7198
0 commit comments