Very simple counting solution O(n) time O(1) space


#1

void Solution::sortColors(vector &A) {

int r=0,w=0,b=0;
for(int i=0;i<A.size();i++){
    if(A[i] == 0) r++;
    if(A[i] == 1) w++;
    if(A[i] == 2) b++;
}
int j=0;
while(r){
    A[j] = 0;
    r--;
    j++;
}
while(w){
    A[j] = 1;
    w--;
    j++;
}
while(b){
    A[j] = 2;
    b--;
    j++;
}
return;

}