n
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
answer:
class Solution {
public:
void sortColors(vector<int>& nums) {
int end0 = -1, end1 = -1, end2 = -1;
for(int i = 0; i < nums.size(); i ++){
if(nums[i] == 0){
nums[++end2] = 2;
nums[++end1] = 1;
nums[++end0] = 0;
}
else if(nums[i] == 1){
nums[++end2] = 2;
nums[++end1] = 1;
}
else nums[++end2] = 2;
}
}
};