2 移动零

大致思路:对数组的元素进行遍历,把不为0的元素放在数组的前面,记录最后一个不为零的元素的下标,然后把之后的元素置为0

代码:

void moveZeroes(int* nums, int numsSize){
    int i=0,j=0;
    if(numsSize==0)
        return;
    
    for(i=0;i<numsSize;i++)
    {
        if(nums[i]!=0)
        {
            nums[j]=nums[i];
            j++;
        }
    }
    while(j<numsSize)
    {
        nums[j]=0;
        j++;
    }

}
原文地址:https://www.cnblogs.com/181118ljh123/p/11600547.html