Share my 4ms c++ solution


  • 0
    A

    the solution is not based on the sort method,and you can only use this as the plusing one.

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int num1=0,num2=0,num3=0;
            int i,j,k;
            for(i=0;i<nums.size();i++)
            {
                if(nums[i]==0) num1++;
                else if(nums[i]==1) num2++;
                else num3++;
            }
            //nums.clear();
            for(i=0;i<num1;i++)
                nums[i]=0;
            for(j=num1;j<(num1+num2);j++)
                nums[j]=1;
            for(k=num2+num1;k<(num3+num2+num1);k++)
                nums[k]=2;
            
        }
    };

Log in to reply
 

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