Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0
, 1
, and 2
to represent the color red, white, and blue respectively.
You should do it in-place (sort numbers in the original array).
Analysis:
Use two pointers p, q, p = 0 and q = A.length - 1. If 0 is found, swap 0 with the number pointed by p, then p++. If 2 is found, swap 2 with the number pointed by q, then q--.
1 public class Solution { 2 public void sortColors(int[] A) { 3 if (A == null || A.length == 0) return; 4 int leftPointer = 0, rightPointer = A.length - 1, current = 0; 5 6 while (current <= rightPointer) { 7 if (A[current] == 0) { 8 swap(A, current, leftPointer); 9 leftPointer++; 10 current++; // we can garantee the left values are valid 11 } else if (A[current] == 2) { 12 swap(A, current, rightPointer); 13 rightPointer--; 14 } else { 15 current++; 16 } 17 } 18 } 19 public void swap(int[] A, int i, int j) { 20 int temp = A[i]; 21 A[i] = A[j]; 22 A[j] = temp; 23 } 24 }
Sort Colors
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.
Given colors=[3, 2, 2, 1, 4]
, k=4
, your code should sort colors in-place to[1, 2, 2, 3, 4]
.
分析: http://www.cnblogs.com/yuzhangcmu/p/4177326.html
inplace,并且O(N)时间复杂度的算法。
我们可以使用类似桶排序的思想,对所有的数进行计数。
1. 从左扫描到右边,遇到一个数字,先找到对应的bucket.比如
3 2 2 1 4
第一个3对应的bucket是index = 2 (bucket从0开始计算)
2. Bucket 如果有数字,则把这个数字移动到i的position(就是存放起来),然后把bucket记为-1(表示该位置是一个计数器,计1)。
3. Bucket 存的是负数,表示这个bucket已经是计数器,直接减1. 并把color[i] 设置为0 (表示此处已经计算过)
4. Bucket 存的是0,与3一样处理,将bucket设置为-1, 并把color[i] 设置为0 (表示此处已经计算过)
5. 回到position i,再判断此处是否为0(只要不是为0,就一直重复2-4的步骤)。
6.完成1-5的步骤后,从尾部到头部将数组置结果。(从尾至头是为了避免开头的计数器被覆盖)
例子(按以上步骤运算):
3 2 2 1 4
2 2 -1 1 4
2 -1 -1 1 4
0 -2 -1 1 4
-1 -2 -1 0 4
-1 -2 -1 -1 0
1 class Solution { 2 /* 3 public void sortColors2(int[] colors, int k) { 4 if (colors == null || colors.length == 0 || k <= 1) return; 5 6 int len = colors.length; 7 for (int i = 0; i < len; i++) { 8 // Means need to deal with A[i] 9 while (colors[i] > 0) { 10 int num = colors[i]; 11 if (colors[num - 1] > 0) { 12 // 1. There is a number in the bucket, 13 // Store the number in the bucket in position i; 14 colors[i] = colors[num - 1]; 15 colors[num - 1] = -1; 16 } else { 17 // 2. Bucket is using or the bucket is empty. 18 colors[num - 1]--; 19 // delete the A[i]; 20 colors[i] = 0; 21 } 22 } 23 } 24 int pointer = colors.length - 1; 25 for (int i = colors.length - 1; i >= 0; i--) { 26 int count = colors[i]; 27 if (count == 0) continue; 28 29 while (count < 0) { 30 colors[pointer--] = i + 1; 31 count++; 32 } 33 } 34 } 35 */ 36 public void sortColors2(int[] colors, int k) { 37 if (colors == null || colors.length == 0 || k <= 1) return; 38 int[] count = new int[k]; 39 int len = colors.length; 40 for (int i = 0; i < len; i++) { 41 count[colors[i] - 1]++; 42 } 43 int index = 0; 44 for (int i = 0; i < count.length; i++) { 45 int p = 0; 46 while (p < count[i]) { 47 colors[index] = i + 1; 48 index++; 49 p++; 50 } 51 } 52 } 53 }