Given an integer array nums
, move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]
Example 2:
Input: nums = [0] Output: [0]
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this is to go through the list and whenever you find a zero, remember it. Then, shift all the non-zero numbers after it towards the front, one by one. Finally, put a zero at the end to cover for the shifted number.
Here's how the algorithm would work step-by-step:
def move_zeroes_brute_force(numbers):
current_index = 0
while current_index < len(numbers):
if numbers[current_index] == 0:
# Found a zero, shift subsequent elements
index_to_shift_from = current_index + 1
while index_to_shift_from < len(numbers):
numbers[index_to_shift_from - 1] = numbers[index_to_shift_from]
index_to_shift_from += 1
# Place a zero at the end to replace shifted number
numbers[len(numbers) - 1] = 0
else:
# Only increment if current element wasn't zero
current_index += 1
The goal is to shift all the zeroes to the end of a list while keeping the other numbers in the same order. We can solve this efficiently by using two different 'markers' that help us keep track of where to place the next non-zero number and what part of the list we've already processed.
Here's how the algorithm would work step-by-step:
def move_zeroes(numbers):
placement_marker = 0
walker_marker = 0
while walker_marker < len(numbers):
# If current number is not zero,
# it needs to be moved to the front.
if numbers[walker_marker] != 0:
numbers[placement_marker], numbers[walker_marker] = numbers[walker_marker], numbers[placement_marker]
# Advance placement to the next available position.
placement_marker += 1
# Always move the walker marker forward
walker_marker += 1
return numbers
Case | How to Handle |
---|---|
Null or empty input array | Return immediately or throw an IllegalArgumentException as appropriate for language conventions. |
Array with only zeroes | The algorithm should still correctly place all zeroes at the end, leaving the array unchanged. |
Array with no zeroes | The algorithm should leave the array unchanged, as all non-zero elements are already in the correct order. |
Array with single element which is zero | Zero will remain at its single index. |
Array with single element which is non-zero | Non-zero element will remain at its single index. |
Array containing large numbers | The solution should handle potentially large numerical values without integer overflow as we are only swapping elements. |
Array with alternating zero and non-zero values | The algorithm will iteratively swap the non-zero values to the front and push the zeroes to the back. |
Large array size | The solution should have linear time complexity O(n), scaling efficiently with a large number of elements by minimizing unnecessary swaps. |