Share C++ solution


  • 0
    A
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            vector<int> counts(3, 0);
            
            for (auto &num: nums)
                ++counts[num];
                
            counts[1] += counts[0];
            counts[2] += counts[1];
                
            int val = 0;
            for (int i=0; i<nums.size(); ++i) {
                while (i == counts[val])
                    ++val;
                    
                nums[i] = val;
            }   
        }
    };

Log in to reply
 

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