Skip to content

Commit 2a4c556

Browse files
authored
Update sort-colors.cpp
1 parent 69f0401 commit 2a4c556

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

C++/sort-colors.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ class Solution {
66
public:
77
void sortColors(vector<int>& nums) {
88
const int target = 1;
9-
for (int i = 0, j = 0, n = nums.size() - 1; j <= n;) {
10-
if (nums[j] < target) {
11-
swap(nums[i++], nums[j++]);
12-
} else if (nums[j] > target) {
13-
swap(nums[j], nums[n--]);
9+
for (int i = 0, left = 0, right = nums.size() - 1; i <= right;) {
10+
if (nums[i] > target) {
11+
swap(nums[i], nums[right--]);
1412
} else {
15-
++j;
13+
if (nums[i] < target) {
14+
swap(nums[left++], nums[i]);
15+
}
16+
++i;
1617
}
1718
}
1819
}

0 commit comments

Comments
 (0)