Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  55] [ 5]  / answers: 1 / hits: 17225  / 9 Years ago, mon, october 26, 2015, 12:00:00

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.


More From » arrays

 Answers
-10

The problem has nothing to do with the return statement, the issue is your algorithm is wrong.



[0,0,1,2,3] will return [0,1,2,3,0]



When you loop in the positive direction and remove indexes, you skip indexes as the next index slides down to the position you already covered.



You need to loop in the negative direction. Start at the end and go to the start.



for (i = nums.length-1; i>=0; i--) {

[#64601] Friday, October 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
averyc

Total Points: 102
Total Questions: 113
Total Answers: 89

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
;