@@ -38,7 +38,9 @@ https://leetcode-cn.com/problems/sort-colors/
38
38
39
39
## 思路
40
40
41
- 这个问题是典型的荷兰国旗问题 ([ https://en.wikipedia.org/wiki/Dutch_national_flag_problem)。 ] ( https://en.wikipedia.org/wiki/Dutch_national_flag_problem%EF%BC%89%E3%80%82 ) 因为我们可以将红白蓝三色小球想象成条状物,有序排列后正好组成荷兰国旗。
41
+ 这个问题是典型的荷兰国旗问题
42
+ ([ https://en.wikipedia.org/wiki/Dutch_national_flag_problem)。 ] ( https://en.wikipedia.org/wiki/Dutch_national_flag_problem%EF%BC%89%E3%80%82 )
43
+ 因为我们可以将红白蓝三色小球想象成条状物,有序排列后正好组成荷兰国旗。
42
44
43
45
## 解法一 - 计数排序
44
46
@@ -51,7 +53,8 @@ https://leetcode-cn.com/problems/sort-colors/
51
53
52
54
## 解法二 - 挡板法
53
55
54
- 我们可以把数组分成三部分,前部(全部是 0),中部(全部是 1)和后部(全部是 2)三个部分。每一个元素(红白蓝分别对应 0、1、2)必属于其中之一。
56
+ 我们可以把数组分成三部分,前部(全部是 0),中部(全部是 1)和后部(全部是 2)三
57
+ 个部分。每一个元素(红白蓝分别对应 0、1、2)必属于其中之一。
55
58
56
59
核心目标就是确定三个部分的分割点,不难知道分割点有两个。
57
60
@@ -63,21 +66,24 @@ https://leetcode-cn.com/problems/sort-colors/
63
66
- end 指向后部开头的前一个位置(刚开始默认后部无 2,所以指向最后一个位置)
64
67
- 遍历指针 current,从头开始进行遍历。
65
68
66
- 形象地来说地话就是有两个挡板,这两个挡板具体在哪事先我们不知道,我们的目标就是移动挡板到合适位置,并且使得挡板间的每一部分都是同一个的颜色。
69
+ 形象地来说地话就是有两个挡板,这两个挡板具体在哪事先我们不知道,我们的目标就是移
70
+ 动挡板到合适位置,并且使得挡板间的每一部分都是同一个的颜色。
67
71
68
72
![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hihivldj31660u0wnb.jpg )
69
73
70
74
还是以题目给的样例来说,初始化挡板位置为最左侧和最右侧:
71
75
72
76
![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hijbh5nj31h80h475x.jpg )
73
77
74
- 读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板,使得 2 跑到挡板的右边。
78
+ 读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板,使得 2 跑到挡板的右边
79
+ 。
75
80
76
81
![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hikpnjhj31s80j4421.jpg )
77
82
78
83
> 带有背景色的圆圈 1 是第一步的意思。
79
84
80
- 并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧元素已就位”。
85
+ 并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧元素已就位
86
+ ”。
81
87
82
88
![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himlg5zj31iu0j8mz8.jpg )
83
89
@@ -87,7 +93,8 @@ https://leetcode-cn.com/problems/sort-colors/
87
93
88
94
![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himzyeaj310m0l2wfs.jpg )
89
95
90
- 这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。空间复杂度为 $O(1),因为我们没有使用额外的空间。
96
+ 这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。空间复杂度为 $O(1),因为我们
97
+ 没有使用额外的空间。
91
98
92
99
### 关键点解析
93
100
@@ -102,24 +109,24 @@ Python3 Code:
102
109
103
110
``` py
104
111
class Solution :
105
- def sortColors (self , nums : List[int ]) -> None :
106
- """
107
- Do not return anything, modify nums in-place instead.
108
- """
109
- p0 = cur = 0
110
- p2 = len (nums) - 1
111
-
112
- while cur <= p2:
113
- if nums[cur] == 0 :
114
- nums[cur], nums[p0] = nums[p0], nums[cur]
115
- p0 += 1
116
- cur += 1
117
- elif nums[cur] == 2 :
118
- nums[cur], nums[p2] = nums[p2], nums[cur]
112
+ def sortColors (self , strs ):
113
+ # p0 是右边界
114
+ # p1 是右边界
115
+ # p2 是左边界
116
+ # p1 超过 p2 结束
117
+ p0, p1, p2 = 0 , 0 , len (strs) - 1
118
+
119
+ while p1 <= p2:
120
+ if strs[p1] == ' blue' :
121
+ strs[p2], strs[p1] = strs[p1], strs[p2]
119
122
p2 -= 1
120
- else :
121
- cur += 1
122
-
123
+ elif strs[p1] == ' red' :
124
+ strs[p0], strs[p1] = strs[p1], strs[p0]
125
+ p0 += 1
126
+ p1 += 1 # p0 一定不是 blue,因此 p1 += 1
127
+ else : # p1 === 'green'
128
+ p1 += 1
129
+ return strs
123
130
```
124
131
125
132
CPP Code:
@@ -171,6 +178,7 @@ class Solution:
171
178
- 时间复杂度:$O(N)$,其中 N 为链表长度。
172
179
- 空间复杂度:$O(1)$。
173
180
174
- 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
175
- 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
181
+ 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我
182
+ 的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K
183
+ star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
176
184
![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hinyr5cj30p00dwt9t.jpg )
0 commit comments