Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d9ca928

Browse files
authored
Update Sort Colors.java
1 parent 42cfbdf commit d9ca928

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

Medium/Sort Colors.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
class Solution {
22
public void sortColors(int[] nums) {
33
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++;
1613
}
1714
}
1815
}
1916

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;
2421
}
2522
}

0 commit comments

Comments
 (0)