Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note: You must do this in-place without making a copy of the array. Minimize the total number of operations
题意:给一个数组,把其中的0放到最后。注意不要创建新的数组和最小化所有操作的数量

void moveZeroes(int* nums, int numsSize) {
//选择排序变一下。。。。。n*2
// int i,j;
// for(i=0;i<numsSize;i++)
// for(j=i+1;j<numsSize;j++){
// if(nums[i]==0){
// int temp=nums[i];
// nums[i]=nums[j];
// nums[j]=temp;
// }
// }
//网上的做法1.简单粗暴容易理解。只不过是分了两步而已。。。
int i;
int index=0;
for(i=0;i<numsSize;i++){
if(nums[i]!=0){
nums[index]=nums[i];
index++;
}
}
// printf("%d",index);
for(i=index;i<numsSize;i++){
nums[i]=0;
}
///还有做法2.不太容易理解,
}PS:维持俩指针。。。。。
一开始想到的事排序。。。。。。把0排到最后,虽然过了,但是复杂度n*2.操作数量也挺多的。
看了网上的做法1.容易理解简单粗暴。
还有一个做法理解起来有点障碍。。。。。。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。