Easy solution (simple counting)


#1

void Solution::sortColors(vector &A) {
int n=A.size();
int z=0,on=0;
for(int i=0;i<n;i++)
{
if(A[i]==0)
z++;
else if(A[i]==1)
on++;
}
int r=0,w=z,b=z+on;
int i=0;
while(i<w)
{
A[i]=0;
i++;
}
while(i<b)
{
A[i]=1;
i++;
}
while(i<n)
{
A[i]=2;
i++;
}
}