class Solution:
def moveZeroes(self, nums):
"""
Do not return anything, modify nums in-place instead.
"""
insert_pos = 0
for num in nums:
if num != 0:
nums[insert_pos] = num
insert_pos += 1
for i in range(insert_pos, len(nums)):
nums[i] = 0
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int insertPos = 0;
for (int num : nums) {
if (num != 0) {
nums[insertPos++] = num;
}
}
for (int i = insertPos; i < nums.size(); ++i) {
nums[i] = 0;
}
}
};
class Solution {
public void moveZeroes(int[] nums) {
int insertPos = 0;
for (int num : nums) {
if (num != 0) {
nums[insertPos++] = num;
}
}
while (insertPos < nums.length) {
nums[insertPos++] = 0;
}
}
}
var moveZeroes = function(nums) {
let insertPos = 0;
for (let num of nums) {
if (num !== 0) {
nums[insertPos++] = num;
}
}
while (insertPos < nums.length) {
nums[insertPos++] = 0;
}
};
You are given an array of integers nums
. Your task is to move all the zeros in the array to the end, while maintaining the relative order of the non-zero elements. This operation must be done in-place (i.e., without making a copy of the array), and you should minimize the total number of operations.
nums
directly.At first glance, you might think about creating a new array and copying over all the non-zero elements, then filling the rest with zeros. However, the problem specifically asks for an in-place solution, so we can't use extra arrays.
The brute-force way would be to repeatedly scan the array and swap zeros with the next non-zero element. But this approach is inefficient and could require many unnecessary operations.
To optimize, we want to minimize writes and only move each element at most once. A good way to do this is to use a "write pointer" or "insert position" that keeps track of where the next non-zero element should go. This way, we can process the array in a single pass and handle all the moves efficiently.
Let's break down the optimal approach step by step:
insertPos
) set to 0. This will keep track of where the next non-zero element should be placed.
nums[insertPos]
, then increment insertPos
. This effectively "compacts" all non-zero elements at the front of the array, in order.
insertPos
to the end) with zeros.
This approach ensures that:
Let's work through an example with nums = [0, 1, 0, 3, 12]
:
[0, 1, 0, 3, 12]
, insertPos = 0
nums[0] = 1
, insertPos = 1
)nums[1] = 3
, insertPos = 2
)nums[2] = 12
, insertPos = 3
)
Array after first pass: [1, 3, 12, 3, 12]
nums[3]
and nums[4]
with 0.
Final array: [1, 3, 12, 0, 0]
Notice how the non-zero elements remain in their original order, and all zeros are moved to the end.
The optimized solution is both time and space efficient, making it suitable for large arrays.
The "Move Zeroes" problem is a classic example of in-place array manipulation. By using a two-pointer strategy, we efficiently move all non-zero elements forward and fill the remainder with zeros, all in a single pass plus a quick cleanup. This approach is both simple and optimal, showcasing how a little planning can lead to elegant and efficient code.