mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
Create 75. Sort Colors.md
This commit is contained in:
parent
b99c474f3e
commit
9e23690994
23
solutions/75. Sort Colors.md
Normal file
23
solutions/75. Sort Colors.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# [75. Sort Colors](https://leetcode.com/problems/sort-colors/)
|
||||||
|
# 思路
|
||||||
|
将一个只包含三种元素的数组进行排序,要求只能遍历一遍,不能使用桶排序(需要两次遍历)。
|
||||||
|
其实这题就是学习数据结构快排时可能遇到的"[荷兰国旗问题](https://en.wikipedia.org/wiki/Dutch_national_flag_problem)",可以用快排类似的partition的方法求解。
|
||||||
|
定义两个指针`right_0`和`left_2`初始分别为`-1`和`nums.size()`。始终满足`right_0`及其左边的元素全为0、`left_2`及其右边的元素全为2。
|
||||||
|
从前往后遍历数组,将0放在位置`right_0 + 1`(通过swap完成,下同)并更新`right_0`, 将2放在位置`left_2 - 1`并更新`left_2`,遍历完成后即所有的0都在左边,所有的2都在右边,中间全为1。
|
||||||
|
时间复杂度O(n),空间复杂度O(1)
|
||||||
|
|
||||||
|
# C++
|
||||||
|
``` C++
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
void sortColors(vector<int>& nums) {
|
||||||
|
int len = nums.size();
|
||||||
|
int i = 0, right_0 = -1, left_2 = len;
|
||||||
|
while(i < left_2){
|
||||||
|
if(nums[i] == 0) swap(nums[i++], nums[++right_0]);
|
||||||
|
else if(nums[i] == 1) i++;
|
||||||
|
else swap(nums[i], nums[--left_2]); // 注意这里不更新i, 因为可能交换后nums[i]=0,需要在下一次循环中再次交换
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user