Skip to content

Instantly share code, notes, and snippets.

@vitkarpov
Created October 15, 2019 08:09
Show Gist options
  • Save vitkarpov/b98ebf1386f721679041427c6ae333da to your computer and use it in GitHub Desktop.
Save vitkarpov/b98ebf1386f721679041427c6ae333da to your computer and use it in GitHub Desktop.
Sort Colors
class Solution {
public:
void sortColors(vector<int>& nums) {
int p0 = 0;
int p1 = 0;
int p2 = nums.size() - 1;
while (p1 <= p2) {
if (nums[p1] == 1) {
p1++;
} else if (nums[p1] == 2) {
swap(nums[p1], nums[p2]);
p2--;
} else {
swap(nums[p0], nums[p1]);
p0++;
p1++;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment