Solutions for sort color easy to understand


  • 0
    E
    public void sortColors(int[] nums) {
            int m=0, count0 =0, count1 = 0, count2 =0;
            
            for(m = 0; m< nums.length; m++){
                if(nums[m] == 0)
                    count0 ++;
                if(nums[m] == 1)
                    count1 ++;
                if(nums[m] == 2)
                    count2 ++;
            }
            for( m = 0; m< nums.length; m++){
             if(count0 !=0){
                    nums[m] =0;
                    count0--;
            }else if(count0==0 && count1 !=0){
                    nums[m] =1;
                    count1--;
            }else if (count1==0 && count2 !=0){
                    nums[m]=2;
                    count2--;
            }
         }
        }
    

Log in to reply
 

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