@@ -47,7 +47,7 @@ https://leetcode-cn.com/problems/sort-colors/
47
47
48
48
这种思路的时间复杂度:$O(n)$,需要遍历数组两次(Two pass)。
49
49
50
- ![ image] ( https://user-images.githubusercontent.com/12479470/83542989-4ef55100-a52e-11ea-9a49-a0e9443da5f4.png )
50
+ ![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hievmxyj30kl0c1t9m.jpg )
51
51
52
52
## 解法二 - 挡板法
53
53
@@ -57,27 +57,27 @@ https://leetcode-cn.com/problems/sort-colors/
57
57
58
58
形象地来说地话就是有两个挡板,这两个挡板实现我们不知道,我们的目标就是移动挡板到合适位置,并且使得挡板每一部分都是合适的颜色。
59
59
60
- ![ image] ( https://user-images.githubusercontent.com/12479470/83542469-9a5b2f80-a52d-11ea-990d-1b56623ba2c8.png )
60
+ ![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hihivldj31660u0wnb.jpg )
61
61
62
62
还是以题目给的样例来说,初始化挡板位置为最左侧和最右侧:
63
63
64
- ![ image] ( https://user-images.githubusercontent.com/12479470/83542548-b19a1d00-a52d-11ea-92aa-c2458d7fe178.png )
64
+ ![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hijbh5nj31h80h475x.jpg )
65
65
66
66
读取第一个元素是 2,它应该在右边,那么我们移动右边地挡板。
67
67
68
- ![ image] ( https://user-images.githubusercontent.com/12479470/83542598-c5de1a00-a52d-11ea-9095-c66e1ed20c8f.png )
68
+ ![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hikpnjhj31s80j4421.jpg )
69
69
70
70
> 带有背景色的圆圈 1 是第一步的意思。
71
71
72
72
并将其和移动挡板后挡板右侧地元素进行一次交换,这意味着“被移动挡板右侧地元素已就位”。
73
73
74
- ![ image] ( https://user-images.githubusercontent.com/12479470/83542711-e9a16000-a52d-11ea-9226-5a385c26174c.png )
74
+ ![ image] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himlg5zj31iu0j8mz8.jpg )
75
75
76
76
。。。
77
77
78
78
整个过程大概是这样的:
79
79
80
- ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1ggssusgyj7j310m0l2td1 .jpg )
80
+ ![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0himzyeaj310m0l2wfs .jpg )
81
81
82
82
这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。
83
83
@@ -144,4 +144,4 @@ class Solution:
144
144
145
145
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
146
146
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
147
- ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs .jpg )
147
+ ![ ] ( https://tva1.sinaimg.cn/large/0081Kckwly1gl0hinyr5cj30p00dwt9t .jpg )
0 commit comments