My 4ms code (C)


  • 0
    Z
    void moveZeroes(int* nums, int numsSize) {
            int k = 0,i = 0;
            for(i=0;i<numsSize;i++){
                if(*(nums+i) == 0){
                    k++;
               }else{
                   *(nums+i-k)=*(nums+i);
                   if(k)
                      *(nums+i) = 0;
               }
           }
    }

  • 0
    S
    void swap(int *a,int *b)
    {
        int t=*a;*a=*b;*b=t;
    }
    
    void moveZeroes(int* nums, int numsSize) {
        int i=0,l=numsSize,j=0;
        if(numsSize == 1)
            return;
        while((i+j)<numsSize)
        {
            if(nums[i] == 0 && nums[i+j+1] != 0)
            {
                swap(&nums[i],&nums[i+j+1]);
                    i++;
    
            }
            else if(nums[i] == 0 && nums[i+j+1] == 0)
            {
                j++;
            }
            else
            {
                i++;
            }
    
        }
    
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.