Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

移动零

lzxjack opened this issue · comments

/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var moveZeroes = function(nums) {
    if(nums.length <= 1) return 
    let left = 0, right = 0, n = nums.length
    while(right < n){ //思路简单,right指针找到所有不为0的数,赋值给left,这样left左边的数都是非零数
        if(nums[right] !== 0){
            nums[left] = nums[right]
            left++
        }
        right++
    }
    while(left < n){
        nums[left++] = 0
    }
};