首页 > 代码库 > LeetCode Sort Colors

LeetCode Sort Colors

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