是否可以在一个解析中按顺序排列仅由1和0组成的数组而不使用辅助数组?
例如:假设您有一个数组a[]={1,0,0,0,1,0,1},为此预期的输出将是a[]={1,1,1,0,0,0,0}.
我编写了下面的C代码,但它找到了2个解析的解决方案.可以优化吗?
void arrange(int a[],int n) {
int i,count=0;
for(i=0;i<n;i++) {
if(a[i]==1)
count++;
a[i]=0;
}
for(i=0;i<count;i++) {
a[i]=1;
}
}
Run Code Online (Sandbox Code Playgroud)