Article From:https://www.cnblogs.com/JTechRoad/p/9060250.html

https://leetcode.com/problems/sort-colors/description/

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

 

Link of this Article: 75. Sort Colors

Leave a Reply

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