zoukankan      html  css  js  c++  java
  • LeeCode-Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

    Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

    Note:
    You are not suppose to use the library's sort function for this problem.

     1 void sortColors(int* nums, int numsSize)
     2 {
     3         if(numsSize==0||nums==NULL)
     4         return;
     5 
     6     if(numsSize==1)
     7         return;
     8 
     9     int red=0,white=0,blue=0;
    10 
    11     for(int i=0;i<numsSize;i++)
    12     {
    13         if(nums[i]==0)
    14             red++;
    15 
    16         if(nums[i]==1)
    17             white++;
    18 
    19         if(nums[i]==2)
    20             blue++;
    21     }
    22     for(int i=0;i<numsSize;i++)
    23     {
    24         if(i<red)
    25         {
    26             nums[i]=0;
    27         }
    28         if(i>=red&&i<(red+white))
    29         {
    30             nums[i]=1;
    31         }
    32         if(i>=(red+white)&&i<numsSize)
    33         {
    34             nums[i]=2;
    35         }
    36 
    37     }
    38 }
  • 相关阅读:
    12.10
    4.06Android使用EditText小技巧汇总
    4.05
    4.04Android学习
    4.03Android学习
    4.02Android学习
    4.01Android学习
    3.31构建之法读后感3
    3.30Android学习
    3.29Android学习
  • 原文地址:https://www.cnblogs.com/vpoet/p/4660508.html
Copyright © 2011-2022 走看看