File tree 3 files changed +78
-0
lines changed
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ Complete solutions to Leetcode problems, updated daily.
45
45
| 047 | [ Permutations II] ( https://github.com/yanglbme/leetcode/tree/master/solution/047.Permutations%20II ) | ` Backtracking ` |
46
46
| 062 | [ Unique Paths] ( https://github.com/yanglbme/leetcode/tree/master/solution/062.Unique%20Paths ) | ` Array ` , ` Dynamic Programming ` |
47
47
| 063 | [ Unique Paths II] ( https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II ) | ` Array ` , ` Dynamic Programming ` |
48
+ | 075 | [ Sort Colors] ( https://github.com/yanglbme/leetcode/tree/master/solution/075.Sort%20Colors ) | ` Array ` , ` Two Pointers ` , ` Sort ` |
49
+ 075.Sort Colors
48
50
| 082 | [ Remove Duplicates from Sorted List II] ( https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II ) | ` Linked List ` |
49
51
| 153 | [ Find Minimum in Rotated Sorted Array] ( https://github.com/yanglbme/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array ) | ` Array ` , ` Binary Search ` |
50
52
Original file line number Diff line number Diff line change
1
+ ## 颜色分类
2
+ ### 题目描述
3
+
4
+ 给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
5
+
6
+ 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
7
+
8
+ 注意:
9
+ 不能使用代码库中的排序函数来解决这道题。
10
+
11
+ 示例:
12
+ ```
13
+ 输入: [2,0,2,1,1,0]
14
+ 输出: [0,0,1,1,2,2]
15
+ ```
16
+
17
+ 进阶:
18
+
19
+ - 一个直观的解决方案是使用计数排序的两趟扫描算法。
20
+ 首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写当前数组。
21
+ - 你能想出一个仅使用常数空间的一趟扫描算法吗?
22
+
23
+ ### 解法
24
+ 指针 p 指示左侧等于 0 区域的最后一个元素,q 指示右侧等于 2 的第一个元素。p, q 初始分别指向 -1, nums.length。
25
+ cur 从 0 开始遍历:
26
+
27
+ - 若 nums[ cur] == 0,则与 p 的下一个位置的元素互换,p+1, cur+1;
28
+ - 若 nums[ cur] == 1,++cur;
29
+ - 若 nums[ cur] == 2,则与 q 的前一个位置的元素互换,q-1,cur不变。
30
+
31
+ ``` java
32
+ class Solution {
33
+ public void sortColors (int [] nums ) {
34
+ int p = - 1 ;
35
+ int q = nums. length;
36
+ int cur = 0 ;
37
+ while (cur < q) {
38
+ if (nums[cur] == 0 ) {
39
+ swap(nums, cur++ , ++ p);
40
+ } else if (nums[cur] == 1 ) {
41
+ ++ cur;
42
+ } else {
43
+ swap(nums, -- q, cur);
44
+ }
45
+ }
46
+ }
47
+
48
+ private void swap (int [] nums , int i , int j ) {
49
+ int t = nums[i];
50
+ nums[i] = nums[j];
51
+ nums[j] = t;
52
+ }
53
+ }
54
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public void sortColors (int [] nums ) {
3
+ int p = -1 ;
4
+ int q = nums .length ;
5
+ int cur = 0 ;
6
+ while (cur < q ) {
7
+ if (nums [cur ] == 0 ) {
8
+ swap (nums , cur ++, ++p );
9
+ } else if (nums [cur ] == 1 ) {
10
+ ++cur ;
11
+ } else {
12
+ swap (nums , --q , cur );
13
+ }
14
+ }
15
+ }
16
+
17
+ private void swap (int [] nums , int i , int j ) {
18
+ int t = nums [i ];
19
+ nums [i ] = nums [j ];
20
+ nums [j ] = t ;
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments