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