https://oj.leetcode.com/problems/sort-colors/
http://blog.csdn.net/linhuanmars/article/details/24286349
Public class solution { public void sortcolors (Int[] A) { // Solution A: sortcolors_countcolor (A); // Solution B: // sortcolors_mergesort (a, 0, a.length - 1); } /////////////////////////// // solution a: countcolor // private void sortcolors_countcolor (Int[] a) { int[] c = new int[3]; for (int i : a) { c[i - 0] ++; } int count = 0; int index = 0; for (int i = 0 ; i < a.length ; i ++) { if (Count == c[index]) { count = 0; index++; i--; continue; } A[i] = index; count ++; } } /////////////////////////// // Solution B: MergeSort // Private void sortcolors_mergesort (Int[] a, int low, int high) { if (Low >= high) return; int mid = (Low + high) / 2; sortcolors_mergesort (A, low, mid); sortcolors_mergesort (A, mid + 1, high); // Merge int[] temp = new int[high - low + 1]; int i = low; int j = mid + 1; int k = 0; while (k < temp.length) { int a = i <= mid ? A[i] : Integer.MAX_VALUE; int b = j <= high ? a[j] : integer.max _value; if (A&NBSP;<&NBSP;B) { temp[k] = a; i++; } else { temp[k] = b; j ++; } k ++; } for (int m = low ; m <= high ; m ++) { a[m] = temp[m - low]; } }}
[Leetcode]75 Sort Colors