Posted on

Description

Submission

class Solution {
public:
    void sortColors(vector<int>& nums) {
        if(nums.size() == 1) return;
        int p0 = 0, p2 = nums.size() - 1;
        
        for(int i =  0; i <= p2;) {
            if(nums[i] == 0) {
                swap(nums[i++], nums[p0++]);
            } else if(nums[i] == 2) {
                swap(nums[i], nums[p2--]);
            } else {
                ++i;
            }
        }
        
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *