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.
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
let moveZeroes = function(nums) {
// let zeroCount=0;
// for(let i=0; i<nums.length; i++){
// if(nums[i]==0){
// zeroCount++;
// }
// }
let arrIdx=0;
for(let j=0; j<nums.length; j++){
if(nums[j]!=0){
if (nums[j]!=nums[arrIdx]){
nums[arrIdx]=nums[j];
}
arrIdx++;
// if(nums.length-arrIdx<=zeroCount){
// break;
// }
}
}
for(let k=arrIdx; k<nums.length; k++){
nums[k]=0;
}
return nums;
};
0을 카운트하는 로직을 처음에 넣었다가 불필요해서 삭제함
~.~