Sort Colors Solutions in Java
Number 75
Difficulty Medium
Acceptance 47.4%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/sort-colors/description//// Author : liuyubobobo/// Time : 2017-11-13// Counting// Time Complexity: O(n)// Space Complexity: O(3)public class Solution1 {public void sortColors(int[] nums) {int[] count = {0, 0, 0};for(int i = 0 ; i < nums.length ; i ++){assert nums[i] >= 0 && nums[i] <= 2;count[nums[i]] ++;}int index = 0;for(int i = 0 ; i < count[0] ; i ++)nums[index++] = 0;for(int i = 0 ; i < count[1] ; i ++)nums[index++] = 1;for(int i = 0 ; i < count[2] ; i ++)nums[index++] = 2;}public static void printArr(int[] nums){for(int num: nums)System.out.print(num + " ");System.out.println();}public static void main(String[] args) {int[] nums = {2, 2, 2, 1, 1, 0};(new Solution1()).sortColors(nums);printArr(nums);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/sort-colors/description//// Author : liuyubobobo/// Time : 2017-11-13// Three Way Quick Sort// Time Complexity: O(n)// Space Complexity: O(1)public class Solution2 {public void sortColors(int[] nums) {int zero = -1; // [0...zero] == 0int two = nums.length; // [two...n-1] == 2for(int i = 0 ; i < two ; ){if(nums[i] == 1)i ++;else if (nums[i] == 2)swap(nums, i, --two);else{ // nums[i] == 0assert nums[i] == 0;swap(nums, ++zero, i++);}}}private void swap(int[] nums, int i, int j){int t = nums[i];nums[i]= nums[j];nums[j] = t;}public static void printArr(int[] nums){for(int num: nums)System.out.print(num + " ");System.out.println();}public static void main(String[] args) {int[] nums = {2, 2, 2, 1, 1, 0};(new Solution2()).sortColors(nums);printArr(nums);}}