[leetcode]Sort Colors

class Solution {
public:
    void sortColors(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int n1 = 0, n2 = 0, n3 = 0;
        
        for(int i = 0; i < n; i++){
            switch(A[i]){
                case 0: n1++; break;
                case 1: n2++; break;
                case 2: n3++; break;
                default: break;
            }
        }
        
        for(int i = 0; i < n1; i++) A[i] = 0;
        for(int i = n1; i < n1+n2; i++) A[i] = 1;
        for(int i = n1+n2; i < n; i++) A[i] = 2;
        
    }
};


原文地址:https://www.cnblogs.com/snake-hand/p/3211855.html