Simple C Solution (O(N))


  • 0
    V
    void moveZeroes(int* nums, int numsSize) {
        int* nextIndex = &nums[0];
        int numUsed = 0;
        for (int i = 0; i < numsSize; i++) {
            if (nums[i] != 0) {
                *nextIndex = nums[i];
                nextIndex++;
                numUsed++;
            }
        }
        memset(nextIndex, 0, (numsSize-numUsed) * sizeof(int));
        return;
    }
    

Log in to reply
 

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