75 - Sort Colors

#medium

Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

You must solve this problem without using the library's sort function.

Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]

class Solution {
public:
    void sortColors(vector<int>& nums) {
        // 排2 排0 剩下0不用排
        if ( nums.size() == 1 ) return;
        int point2 = nums.size() - 1;
        int point0 = 0, i = 0;
        while ( i <= point2 ) {
            if ( nums[i] == 2 ) { 
                swap( nums[point2], nums[i] );
                point2--;
                // 換完可能是0,前面有1,i不動。
            }
            else if ( nums[i] == 1 ) i++;
            else {
                swap( nums[point0], nums[i] );
                point0++;
                i++;
            }

        }

    }
};