Skip to content

Commit 70e043e

Browse files
committed
75. Sort Colors
1 parent a4388ac commit 70e043e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

sort-colors.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Runtime: 3 ms
2+
class Solution {
3+
public:
4+
void sortColors(vector<int>& nums) {
5+
int two = nums.size() - 1;
6+
int zero = 0;
7+
8+
for (int i = 0; i <= two; i++)
9+
{
10+
while (nums[i] == 2 && i < two)
11+
swap(nums[i], nums[two--]);
12+
13+
while (nums[i] == 0 && i > zero)
14+
swap(nums[i], nums[zero++]);
15+
}
16+
}
17+
};

0 commit comments

Comments
 (0)