I encountered an issue while working on the leetcode 283 move zeroes problem where I faced a strange test failure when there are two zeros next to each other.
Here is the code snippet I used:
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
let i = 0;
while(i < nums.length){
if(nums[i] == 0){
nums.push(nums.splice(i, 1))
}
i++
}
};
All tests passed except for cases where consecutive zeros occur. The input and expected outputs are as follows:
Input nums = [0,0,1,3,12] Output [0,1,3,12,0] Expected [1,3,12,0,0]
Input nums = [0,0,0,0,0,1,0] Output [0,0,1,0,0,0,0] Expected [1,0,0,0,0,0,0]
If you could provide any hints or suggestions on what might have gone wrong, I would greatly appreciate it.