Counting_sort,STL,vector, 4ms C++ with comments


  • 2
    S
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int n_red=0,n_white=0,n_blue=0;
            for ( auto c: nums)//iterate numbers in the vector
            switch(c)
            {
                case 0:
                n_red++;
                break;
                case 1:
                n_white++;
                break;
                case 2:
                n_blue++;
            }
            fill(nums.begin(),nums.begin()+n_red,0);//put back numbers by order
            fill(nums.begin()+n_red,nums.begin()+n_red+n_white,1);
            fill(nums.begin()+n_red+n_white,nums.end(),2);
    
    
            
        }
    };

  • -2
    O

    bravo! gooooooooooooood answer


Log in to reply
 

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