Clean c++ solution to sort k colors


  • 2
    void sortColors(vector<int>& nums) {
        int k = 3;
        vector<int> colors(k);
        int size = nums.size();
        for (int i = 0; i < size; i++) {
            int current = k, num = nums[i];
            while (--current >= num)
                nums[colors[current]++] = current;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.