publicclassSolution { /** * @param nums: A list of integer which is 0, 1 or 2 * @return: nothing */publicvoidsortColors(int[] nums) {quickSort(nums,1);quickSort(nums,2); }privatevoidquickSort(int[] nums,int k) {if (nums ==null) return;int i =0, j =nums.length-1;while (i <= j) {while (i <= j && nums[i] < k) { i++; }while (i <= j && nums[j] >= k) { j--; }if (i <= j) {int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } }}
Notes
Quick sort
Solution 4 (Java)
publicclassSolution { /** * @param nums: A list of integer which is 0, 1 or 2 * @return: nothing */publicvoidsortColors(int[] nums) {Arrays.sort(nums); }}
Notes
Not really a "solution"...
Solution 5 (Java)
publicclassSolution { /** * @param nums: A list of integer which is 0, 1 or 2 * @return: nothing */publicvoidsortColors(int[] nums) {int i =0, j =0, k =nums.length-1;while (j <= k) {if (nums[j] ==0) {swap(nums, j, i); i++; j++; } elseif (nums[j] ==1) { j++; } else {swap(nums, j, k); k--; } } }privatevoidswap(int[] nums,int a,int b) {int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; }}// [0, i) all 0's// [i, j) all 1's// [j, k) exploring// [k, nums.length - 1) all 2's