C solution


  • 0
    M

    void sortColors(int* nums, int numsSize)
    {
    int i,red_count = 0,white_count = 0,blue_count = 0;
    for( i=0;i<numsSize;i++)
    {
    switch(nums[i])
    {
    case 0:
    red_count++;
    break;
    case 1:
    white_count++;
    break;
    case 2:
    blue_count++;
    break;
    }
    }
    for( i = 0;i< red_count ;i++)
    {
    nums[i] = 0;
    }
    for( i = red_count;i< white_count+red_count ;i++)
    {
    nums[i] = 1;
    }
    for( i = red_count+white_count;i< numsSize;i++)
    {
    nums[i] = 2;
    }
    }


Log in to reply
 

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