File tree 1 file changed +10
-13
lines changed
1 file changed +10
-13
lines changed Original file line number Diff line number Diff line change @@ -2,21 +2,18 @@ class Solution {
2
2
public void sortColors (int [] nums ) {
3
3
int zeroIdx = 0 ;
4
4
int twoIdx = nums .length - 1 ;
5
- int currIdx = 0 ;
6
- while (currIdx <= twoIdx ) {
7
- if (nums [currIdx ] == 0 ) {
8
- swap (nums , currIdx ++, zeroIdx ++);
9
- } else if (nums [currIdx ] == 2 ) {
10
- swap (nums , currIdx , twoIdx --);
5
+ for (int i = 0 ; i <= twoIdx ; ) {
6
+ if (nums [i ] == 0 && i != zeroIdx ) {
7
+ int temp = nums [zeroIdx ];
8
+ nums [zeroIdx ++] = nums [i ];
9
+ nums [i ] = temp ;
10
+ } else if (nums [i ] == 2 && i != twoIdx ) {
11
+ int temp = nums [twoIdx ];
12
+ nums [twoIdx --] = nums [i ];
13
+ nums [i ] = temp ;
11
14
} else {
12
- currIdx ++;
15
+ i ++;
13
16
}
14
17
}
15
18
}
16
-
17
- private void swap (int [] nums , int idxOne , int idxTwo ) {
18
- int temp = nums [idxTwo ];
19
- nums [idxTwo ] = nums [idxOne ];
20
- nums [idxOne ] = temp ;
21
- }
22
19
}
You can’t perform that action at this time.
0 commit comments