-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSortColors.java
More file actions
32 lines (32 loc) · 818 Bytes
/
SortColors.java
File metadata and controls
32 lines (32 loc) · 818 Bytes
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
public class Solution {
public void sortColors(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int n = A.length;
int red = 0;
int white = n - 1;
int i = 0;
while(i < n){
int temp = -1;
if(A[i] == 0){
if (i < red){
i ++;
continue;
}
temp = A[red];
A[red] = A[i];
A[i] = temp;
red++;
}
else if (A[i] == 2 && i < white){
temp = A[white];
A[white] = A[i];
A[i] = temp;
white--;
}
else{
i++;
}
}
}
}