移动零
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
}
};