-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort Colors.cpp
42 lines (39 loc) · 988 Bytes
/
Sort Colors.cpp
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
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int head = 0;
int whead = 0;
int tail = n-1;
int wtail = n-1;
while (head<=tail) {
int c=A[head];
A[head]=1;
if (c==0) {
A[whead] = 0;
whead++;
head++;
continue;
}
if (c==1) {
head++;
continue;
}
if (c==2) {
A[head]=A[tail];
A[tail]=2;
}
if (A[tail]==2) {
A[wtail] = 2;
wtail--;
tail--;
continue;
}
if (A[tail]==1) {
tail--;
continue;
}
}
}
};