@@ -38,7 +38,9 @@ https://leetcode-cn.com/problems/sort-colors/
3838
3939## 思路
4040
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+ 因为我们可以将红白蓝三色小球想象成条状物,有序排列后正好组成荷兰国旗。
4244
4345## 解法一 - 计数排序
4446
@@ -51,7 +53,8 @@ https://leetcode-cn.com/problems/sort-colors/
5153
5254## 解法二 - 挡板法
5355
54- 我们可以把数组分成三部分,前部(全部是 0),中部(全部是 1)和后部(全部是 2)三个部分。每一个元素(红白蓝分别对应 0、1、2)必属于其中之一。
56+ 我们可以把数组分成三部分,前部(全部是 0),中部(全部是 1)和后部(全部是 2)三
57+ 个部分。每一个元素(红白蓝分别对应 0、1、2)必属于其中之一。
5558
5659核心目标就是确定三个部分的分割点,不难知道分割点有两个。
5760
@@ -63,21 +66,24 @@ https://leetcode-cn.com/problems/sort-colors/
6366- end 指向后部开头的前一个位置(刚开始默认后部无 2,所以指向最后一个位置)
6467- 遍历指针 current,从头开始进行遍历。
6568
66- 形象地来说地话就是有两个挡板,这两个挡板具体在哪事先我们不知道,我们的目标就是移动挡板到合适位置,并且使得挡板间的每一部分都是同一个的颜色。
69+ 形象地来说地话就是有两个挡板,这两个挡板具体在哪事先我们不知道,我们的目标就是移
70+ 动挡板到合适位置,并且使得挡板间的每一部分都是同一个的颜色。
6771
6872![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hihivldj31660u0wnb.jpg )
6973
7074还是以题目给的样例来说,初始化挡板位置为最左侧和最右侧:
7175
7276![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hijbh5nj31h80h475x.jpg )
7377
74- 读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板,使得 2 跑到挡板的右边。
78+ 读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板,使得 2 跑到挡板的右边
79+ 。
7580
7681![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hikpnjhj31s80j4421.jpg )
7782
7883> 带有背景色的圆圈 1 是第一步的意思。
7984
80- 并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧元素已就位”。
85+ 并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧元素已就位
86+ ”。
8187
8288![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himlg5zj31iu0j8mz8.jpg )
8389
@@ -87,7 +93,8 @@ https://leetcode-cn.com/problems/sort-colors/
8793
8894![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himzyeaj310m0l2wfs.jpg )
8995
90- 这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。空间复杂度为 $O(1),因为我们没有使用额外的空间。
96+ 这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。空间复杂度为 $O(1),因为我们
97+ 没有使用额外的空间。
9198
9299### 关键点解析
93100
@@ -102,24 +109,24 @@ Python3 Code:
102109
103110``` py
104111class 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]
119122 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
123130```
124131
125132CPP Code:
@@ -171,6 +178,7 @@ class Solution:
171178- 时间复杂度:$O(N)$,其中 N 为链表长度。
172179- 空间复杂度:$O(1)$。
173180
174- 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
175- 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
181+ 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我
182+ 的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K
183+ star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
176184![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hinyr5cj30p00dwt9t.jpg )
0 commit comments