forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
75.c
78 lines (68 loc) · 1.85 KB
/
75.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
void swap(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
/* Dutch national flag problem.
* three way partition, just like radix sort
*/
void sortColors(int* nums, int numsSize) {
int i, zero, two;
i = 0;
zero = 0;
two = numsSize - 1;
while (i <= two) {
if (nums[i] == 0) {
swap(&nums[i], &nums[zero++]);
i++; /* index i scans from left to right, so we don't need to check
* if left side have 2, just swap and move forward.
* And, if the numbers are all 0, we must move forward,
* otherwise we will loop forever.
*/
}
else if (nums[i] == 2) {
swap(&nums[i], &nums[two--]);
/* right side may have 0, so we stop at this position and check
* repeatedly. the loop will exit if pointer two is smaller than i.
*/
}
else {
i++;
}
}
}
/* simply fill the numbers, O(n), two-pass */
void sortColors0(int* nums, int numsSize) {
int i, zero_count, one_count;
zero_count = 0;
one_count = 0;
for (i = 0; i < numsSize; i++) {
if (nums[i] == 0) {
zero_count++;
}
if (nums[i] == 1) {
one_count++;
}
}
for (i = 0; i < zero_count; i++) {
nums[i] = 0;
}
for (i = zero_count; i < zero_count + one_count; i++) {
nums[i] = 1;
}
for ( ; i < numsSize; i++) {
nums[i] = 2;
}
}
int main() {
int nums[] = { 1, 2, 0, 1, 0, 2, 0, 0, 1, 0, 2, 1 };
int size = sizeof(nums) / sizeof(nums[0]);
sortColors(nums, size);
int i;
for (i = 0; i < size; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}