Leetcode 75 Solution

This article provides solution to leetcode question 75 (sort-colors)

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

Solution

class Solution {
public:
    void sortColors(vector<int>& nums) {
        int l = 0;
        int r = nums.size() - 1;

        for (int i = 0; i <= r; i++)
        {
            if (nums[i] == 0)
                swap(nums[l++], nums[i]);
            else if (nums[i] == 2)
                swap(nums[r--], nums[i]), i--;
        }
    }
};