문제 링크 : Move Zeroes
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
const nonZeroLeng = nums.filter(el => el !== 0).length;
for(let i=0; i<nums.length; i++) {
if(i === nonZeroLeng) {
break;
}
if(nums[i] === 0) {
nums.splice(i,1)
nums.push(0)
i--
}
}
};
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
let index = 0;
for(let i = 0; i < nums.length; i++){
if(nums[i] != 0){
let temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
index++
}
}
};
[ 0, 1, 0, 3, 12 ]
[ 1, 0, 0, 3, 12 ]
[ 1, 0, 0, 3, 12 ]
[ 1, 3, 0, 0, 12 ]
[ 1, 3, 12, 0, 0 ]