Learn DSA
Depth-First Search
Greedy Algorithms
Two Pointers
Move Zeroes
easy
Watch Video Walkthrough
Watch a video walking through the coding problem step-by-step
Watch Video Walkthrough
Watch a video walking through the coding problem step-by-step
DESCRIPTION (credit Leetcode.com)
Given an integer array nums, write a function to rearrange the array by moving all zeros to the end while keeping the order of non-zero elements unchanged. Perform this operation in-place without creating a copy of the array.
Input:
Output:
💻 Desktop Required
The code editor works best on larger screens. Please open this page on your computer to write and run code.
"Write a function that moves all zeroes in an integer array `nums` to the end of the array while maintaining the relative order of the non-zero elements."
Run your code to see results here
Have suggestions or found something wrong?
Explanation
Solution
def moveZeroes(nums):nextNonZero = 0for i in range(len(nums)):if nums[i] != 0:nums[nextNonZero], nums[i] = nums[i], nums[nextNonZero]nextNonZero += 1
move zeros
0 / 13
Login to track your progress
Your account is free and you can post anonymously if you choose.