-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path75.sortColors.cpp
More file actions
executable file
·62 lines (59 loc) · 1.47 KB
/
Copy path75.sortColors.cpp
File metadata and controls
executable file
·62 lines (59 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
@filename 75.sortColors.cpp
@author caonan
@date 2022-04-26 15:42:12
@reference leetcode
@url https://leetcode-cn.com/problems/sort-colors/
@brief 给定一个包含红色、白色和蓝色、共 n
个元素的数组 nums ,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
必须在不使用库的sort函数的情况下解决这个问题。
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
// 单指针两次遍历
void sortColors(vector<int> &nums) {
int head_index = 0;
for (auto &n : nums) {
if (n == 0) {
std::swap(nums[head_index++], n);
}
}
for (auto &n : nums) {
if (n == 1) {
std::swap(nums[head_index++], n);
}
}
}
};
class Solution1 : public Solution {
public:
// 双指针单次遍历
void sortColors(vector<int> &nums) {
int p0 = 0, p1 = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 1) {
swap(nums[i], nums[p1++]);
} else if (nums[i] == 0) {
swap(nums[i], nums[p0]);
if (p0 < p1) {
swap(nums[i], nums[p1]);
}
p0++;
p1++;
}
}
}
};
int main() { return 0; }