Share my solution with counting the numbers of color, no swap


  • 0
    W

    just count the numbers of color and print them out.

    void sortColors(int A[], int n) {
        int num_red=0;
        int num_white=0;
        int i;
        for(i=0;i<n;i++)
        {
            if(A[i]==0)
                num_red++;
            else if(A[i]==1)
                num_white++;
        }
        for(i=0;i<n;i++)
        {
            if(i<num_red)
                A[i]=0;
            else if (i>=num_red && i<(num_red+num_white))
                A[i]=1;
            else
                A[i]=2;
        }
    
    }

Log in to reply
 

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