My simple C Solution, I don't understand why it run such slowly since I have just a for loop. Anybody can help?Thanks!


  • 0
    1
    void moveZeroes(int* nums, int numsSize) {
    
     int j = 0, temp = 0;
    for(int i = 0; i < numsSize; i++){
        //find the first zero and mark it all the time
        if(nums[i] == 0 && nums[j] != 0)
        {
            j = i;    
        }
        //if the element is not zero, change it with the first zero
        if(nums[i] != 0)
        {
            temp = nums[i];
        	nums[i]= nums[j];
        	nums[j] = temp;
        	//move to the next zero and becmoe the first zero
            j++;
        }
    }
    

    }


Log in to reply
 

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