I am attempting to complete a leetcode.com question in JS. I am new to algorithms in general, and am having some trouble getting my first submission accepted.
The question specifies the following:
Given an array nums
, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note: You must do this in-place without making a copy of the array. Minimize the total number of operations.
With that, here is my code:
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
var i, temp;
for (i = 0; i < nums.length-1; i++) {
if(nums[i] === 0) {
temp = nums.splice(i, 1);
nums.push(temp[0]);
}
}
return null;
};
The comments at the top of the code sample are provided in their text editor, and led me to believe that I am not supposed to provide any return statement. Though the validator on their site seemed to not want to accept that at all, so I started returning null...
When I log my nums
to the console after handling the input, I am seeing the desired result nums = [1, 3, 12, 0, 0]
. Regardless my answer keeps getting rejected. I would love to understand what I am doing wrong here, so I can fix it.
I understand this may be a duplicate. I saw other responses dealing with C and Java, but none I saw dealt with JS.
return null
altogether.nums.splice(i,1); nums.push(0)
.