Created
October 15, 2019 08:09
-
-
Save vitkarpov/b98ebf1386f721679041427c6ae333da to your computer and use it in GitHub Desktop.
Sort Colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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