- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathSortColors75.java
More file actions
Latest commit
189 lines (161 loc) · 4.81 KB
/
Copy pathSortColors75.java
File metadata and controls
189 lines (161 loc) · 4.81 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Given an array with n objects colored red, white or blue, sort them so that
* objects of the same color are adjacent, with the colors in the order red,
* white and blue.
*
* Here, we will use the integers 0, 1, and 2 to represent the color red, white,
* and blue respectively.
*
* Note:
* You are not suppose to use the library's sort function for this problem.
*
* Follow up:
* A rather straight forward solution is a two-pass algorithm using counting sort.
* First, iterate the array counting number of 0's, 1's, and 2's, then overwrite
* array with total number of 0's, then 1's and followed by 2's.
*
* Could you come up with an one-pass algorithm using only constant space?
*/
importjava.util.Arrays;
publicclassSortColors75 {
publicvoidsortColors(int[] nums) {
intL = nums.length;
if (L == 0 || L == 1) {
return;
}
helper(0, L - 1, nums);
}
privatevoidhelper(intleft, intright, int[] nums) {
if (left == right) {
return;
}
intmid = (left + right) / 2;
helper(left, mid, nums);
helper(mid + 1, right, nums);
merge(left, mid, right, nums);
}
privatevoidmerge(intleft, intmid, intright, int[] nums) {
intl = 0;
intr = 0;
inti = left;
int[] la = Arrays.copyOfRange(nums, left, mid+1);
int[] ra = Arrays.copyOfRange(nums, mid+1, right+1);
while (l <= mid - left && r <= right - mid - 1) {
if (la[l] <= ra[r]) {
nums[i] = la[l];
l++;
} else {
nums[i] = ra[r];
r++;
}
i++;
}
while (l <= mid - left) {
nums[i] = la[l];
l++;
i++;
}
}
/**
* https://discuss.leetcode.com/topic/6968/four-different-solutions
*/
// two pass O(m+n) space
publicvoidsortColors2(intA[]) {
intnum0 = 0, num1 = 0, num2 = 0;
for(inti = 0; i < A.length; i++) {
if (A[i] == 0) ++num0;
elseif (A[i] == 1) ++num1;
elseif (A[i] == 2) ++num2;
}
for(inti = 0; i < num0; ++i) A[i] = 0;
for(inti = 0; i < num1; ++i) A[num0+i] = 1;
for(inti = 0; i < num2; ++i) A[num0+num1+i] = 2;
}
// one pass in place solution
publicvoidsortColors3(intA[]) {
intn0 = -1, n1 = -1, n2 = -1;
for (inti = 0; i < A.length; ++i) {
if (A[i] == 0)
{
A[++n2] = 2; A[++n1] = 1; A[++n0] = 0;
}
elseif (A[i] == 1)
{
A[++n2] = 2; A[++n1] = 1;
}
elseif (A[i] == 2)
{
A[++n2] = 2;
}
}
}
// one pass in place solution
publicvoidsortColors4(intA[]) {
intj = 0, k = A.length - 1;
for (inti = 0; i <= k; ++i){
if (A[i] == 0 && i != j)
swap(A, i--, j++);
elseif (A[i] == 2 && i != k)
swap(A, i--, k--);
}
}
// one pass in place solution
publicvoidsortColors5(intA[], intn) {
intj = 0, k = n-1;
for (inti=0; i <= k; i++) {
if (A[i] == 0)
swap(A, i, j++);
elseif (A[i] == 2)
swap(A, i--, k--);
}
}
// bucket sort
publicvoidsortColors6(int[] nums) {
int[] buckets = newint[3];
for (inti=0; i<nums.length; i++) {
buckets[nums[i]]++;
}
intidx = 0;
for (inti=0; i<3; i++) {
intcount = buckets[i];
for (intj=0; j<count; j++) {
nums[idx++] = i;
}
}
}
publicvoidsortColors7(int[] nums) {
if (nums == null || nums.length <= 1) return;
intleft = 0;
intright = nums.length - 1;
inti = left;
while (i <= right) {
if (nums[i] == 0) {
swap(nums, i++, left++);
} elseif (nums[i] == 2) {
swap(nums, i, right--);
} else {
i++;
}
}
}
privatevoidswap(int[] nums, inti, intj) {
inttmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
publicstaticvoidmain(String[] args) {
SortColors75sc = newSortColors75();
int[] nums = newint[]{
5, 8, 1, 3, 7, 2, 9, 4, 1, 1, 0
};
System.out.println(Arrays.toString(nums));
sc.sortColors(nums);
System.out.println(Arrays.toString(nums));
nums = newint[]{
1, 1, 0, 2, 0, 1, 2, 0, 1, 1, 1, 2
};
System.out.println(Arrays.toString(nums));
sc.sortColors(nums);
System.out.println(Arrays.toString(nums));
}
}