clueless coding // TODO: be smarter

LeetCode Sort Colors: CPP Solution



TL;DR Code Solution


void sortColors(vector<int>& nums) {

    int j = 0, k = (int)nums.size() - 1;
    for (int i=0; i <= k; i++) {
        if (nums[i] == 0)
            swap(nums[i], nums[j++]);
        else if (nums[i] == 2)
            swap(nums[i--], nums[k--]);
    }

}