Simple solution using 3 variables


#1

void Solution::sortColors(vector &A) {

int count1=0,count2=0,count3=0;
for(int i=0;i<A.size();i++){
if(A[i]==0) count1++;
else if(A[i]==1) count2++;
else count3++;
}
int k=0;
while(count1!=0){
A[k]=0;
k++;
count1–;
}
while(count2!=0){
A[k]=1;
k++;
count2–;
}
while(count3!=0){
A[k]=2;
k++;
count3–;
}
}