Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

移动零 #366

Open
lzxjack opened this issue Jan 11, 2023 · 1 comment
Open

移动零 #366

lzxjack opened this issue Jan 11, 2023 · 1 comment

Comments

@lzxjack
Copy link
Contributor

lzxjack commented Jan 11, 2023

No description provided.

@lxy-Jason
Copy link
Contributor

/**
 * @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
    }
};

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants