75. 颜色分类
为保证权益,题目请参考 75. 颜色分类(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
void sortColors(vector<int> &nums) {
int l = 0;
int r = nums.size() - 1;
int cur = 0;
while (cur <= r) {
if (nums[cur] == 0) {
int t = nums[l];
nums[l] = nums[cur];
nums[cur] = t;
l++;
cur++;
} else if (nums[cur] == 2) {
int t = nums[r];
nums[r] = nums[cur];
nums[cur] = t;
r--;
}else{
cur++;
}
}
}
};
int main() {
}
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
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