75. Sort Colors
1 class Solution { 2 public void sortColors(int[] nums) { 3 if(nums == null || nums.length == 0) return; 4 int count0 = 0; 5 int count1 = 0; 6 int count2 = 0; 7 for(int i = 0; i < nums.length; i++) { 8 if(nums[i] == 0) { 9 count0++; 10 }else if(nums[i] == 1) { 11 count1++; 12 }else { 13 count2++; 14 } 15 } 16 int i = 0; 17 for(i = 0; i < count0; i++) { 18 nums[i] = 0; 19 } 20 for(i = count0; i < count0 + count1; i++) { 21 nums[i] = 1; 22 } 23 for( ; i < nums.length; i++) { 24 nums[i] = 2; 25 } 26 27 } 28 }