class Solution { public: void sortColors(int A[], int n) { int cnt[3] = {0}; for (int i = 0; i < n; i++) { cnt[A[i]]++; } int start = 0; for (int i=0; i<3; i++) { if (i > 0) start += cnt[i-1]; for (int j=start; j < start + cnt[i]; j++) { A[j] = i; } } } };
简化版计数排序,two-pass就two-pass
LeetCode Sort Colors,布布扣,bubuko.com
原文:http://www.cnblogs.com/lailailai/p/3851896.html