- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsortColors.java
More file actions
Latest commit
38 lines (33 loc) · 817 Bytes
/
Copy pathsortColors.java
File metadata and controls
38 lines (33 loc) · 817 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
33
34
35
36
37
38
//计数排序需要扫描两遍
//类似于快排,只扫描一遍数组
publicclassSolution {
voidswap(int [] A, inti, intj) {
inttmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
publicvoidsortColors(int[] A) {
if(A==null||A.length==0) {
return;
}
intbegin = 0;
intend = A.length - 1;
intiter = 0;
while(iter<=end) {
if(A[iter]==0) {
swap(A,iter,begin);
iter++;
begin++;
continue;
}
if(A[iter]==2) {
swap(A,iter,end);
end--;
continue;
}
if(A[iter]==1) {
iter++;
}
}
}
}