Given an array with n objects colored red, white or blue, sort them so, objects of the same color is Adjacen T, with the colors in the order red, white and blue.
Here, we'll use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You is not a suppose to use the library's sort function for this problem.
Problem Solving Ideas:
Fast line speed is too slow, rather than directly with two pointers to mark, 0 to left 2 on the right side, Java implementation is as follows:
public void Sortcolors (int[] nums) { int left = 0, right = nums.length-1;for (int i = 0; I <= right;) {if (nums[i] = = 0 && i > left) {int temp = Nums[i];nums[i] = Nums[left];nums[left] = temp;left++;} else if (num S[i] = = 2 && i < right) {int temp = Nums[i];nums[i] = nums[right];nums[right] = temp;right--;} elsei++;} }
Java for Leetcode 075 Sort Colors