Skip to content

Commit 81f4b13

Browse files
feat: azl397985856#75 A better solution with O(n) (azl397985856#280)
* Update 75: A better solution with O(n) * Update 75.sort-colors.md * Update 75.sort-colors.md Co-authored-by: lucifer <azl397985856@gmail.com>
1 parent 1d751e6 commit 81f4b13

File tree

1 file changed

+50
-72
lines changed

1 file changed

+50
-72
lines changed

problems/75.sort-colors.md

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,55 @@ First, iterate the array counting number of 0's, 1's, and 2's, then overwrite ar
1919
Could you come up with a one-pass algorithm using only constant space?
2020

2121
## 思路
22+
这个问题是典型的荷兰国旗问题 (https://en.wikipedia.org/wiki/Dutch_national_flag_problem)。 因为我们可以将红白蓝三色小球想象成条状物,有序排列后正好组成荷兰国旗。
2223

23-
其实就是排序,而且没有要求稳定性,就是用啥排序算法都行。
24-
题目并没有给出数据规模,因此我默认数据量不大,直接选择了冒泡排序
25-
26-
## 关键点解析
27-
28-
冒泡排序的时间复杂度是N平方,无法优化,但是可以进一步优化常数项,
29-
比如循环的起止条件。 由于每一次遍历都会将最后一位“就位”,因此内层循环的截止条件就可以是
30-
`nums.length - i`, 而不是 `nums.length`, 可以省一半的时间。
31-
32-
33-
## 代码
34-
35-
```js
36-
/*
37-
* @lc app=leetcode id=75 lang=javascript
38-
*
39-
* [75] Sort Colors
40-
*
41-
* https://leetcode.com/problems/sort-colors/description/
42-
*
43-
* algorithms
44-
* Medium (41.41%)
45-
* Total Accepted: 297K
46-
* Total Submissions: 716.1K
47-
* Testcase Example: '[2,0,2,1,1,0]'
48-
*
49-
* Given an array with n objects colored red, white or blue, sort them in-place
50-
* so that objects of the same color are adjacent, with the colors in the order
51-
* red, white and blue.
52-
*
53-
* Here, we will use the integers 0, 1, and 2 to represent the color red,
54-
* white, and blue respectively.
55-
*
56-
* Note: You are not suppose to use the library's sort function for this
57-
* problem.
58-
*
59-
* Example:
60-
*
61-
*
62-
* Input: [2,0,2,1,1,0]
63-
* Output: [0,0,1,1,2,2]
64-
*
65-
* Follow up:
66-
*
67-
*
68-
* A rather straight forward solution is a two-pass algorithm using counting
69-
* sort.
70-
* First, iterate the array counting number of 0's, 1's, and 2's, then
71-
* overwrite array with total number of 0's, then 1's and followed by 2's.
72-
* Could you come up with a one-pass algorithm using only constant space?
73-
*
74-
*
75-
*/
76-
/**
77-
* @param {number[]} nums
78-
* @return {void} Do not return anything, modify nums in-place instead.
79-
*/
80-
var sortColors = function(nums) {
81-
function swap(nums, i, j) {
82-
const temp = nums[i];
83-
nums[i] = nums[j];
84-
nums[j] = temp;
85-
}
86-
for (let i = 0; i < nums.length - 1; i++) {
87-
for (let j = 0; j < nums.length - i; j++) {
88-
if (nums[j] < nums[j -1]) {
89-
swap(nums, j - 1 , j)
90-
}
91-
92-
}
93-
}
94-
};
24+
有两种解决思路。
25+
26+
## 解法一
27+
- 遍历数组,统计红白蓝三色球(0,1,2)的个数
28+
- 根据红白蓝三色球(0,1,2)的个数重排数组
29+
30+
这种思路的时间复杂度:$O(n)$,需要遍历数组两次。
31+
32+
## 解法二
33+
34+
我们可以把数组分成三部分,前部(全部是0),中部(全部是1)和后部(全部是2)三个部分。每一个元素(红白蓝分别对应0、1、2)必属于其中之一。将前部和后部各排在数组的前边和后边,中部自然就排好了。
35+
36+
我们用三个指针,设置两个指针begin指向前部的末尾的下一个元素(刚开始默认前部无0,所以指向第一个位置),end指向后部开头的前一个位置(刚开始默认后部无2,所以指向最后一个位置),然后设置一个遍历指针current,从头开始进行遍历。
37+
38+
这种思路的时间复杂度也是$O(n)$, 只需要遍历数组一次。
39+
40+
### 关键点解析
41+
42+
43+
- 荷兰国旗问题
44+
- counting sort
45+
46+
### 代码
47+
48+
代码支持: Python3
49+
50+
Python3 Code:
51+
52+
``` python
53+
class Solution:
54+
def sortColors(self, nums: List[int]) -> None:
55+
"""
56+
Do not return anything, modify nums in-place instead.
57+
"""
58+
p0 = cur = 0
59+
p2 = len(nums) - 1
60+
61+
while cur <= p2:
62+
if nums[cur] == 0:
63+
nums[cur], nums[p0] = nums[p0], nums[cur]
64+
p0 += 1
65+
cur += 1
66+
elif nums[cur] == 2:
67+
nums[cur], nums[p2] = nums[p2], nums[cur]
68+
p2 -= 1
69+
else:
70+
cur += 1
9571
```
72+
73+

0 commit comments

Comments
 (0)