HZFE / algorithms-solving

1 stars 0 forks source link

2022-08-08 #10

Open github-actions[bot] opened 2 years ago

github-actions[bot] commented 2 years ago

283. Move Zeroes

gongpeione commented 2 years ago

283 Move Zeroes

/*
 * @lc app=leetcode id=283 lang=typescript
 *
 * [283] Move Zeroes
 */

// @lc code=start
/**
 Do not return anything, modify nums in-place instead.
 */
function moveZeroes(nums: number[]): void {
    let pointerA = 0;

    for (let pointerB = 0; pointerB < nums.length; pointerB++) {
        // if nums[pointerB] not equals to zero, then swap nums[pointerA] and nums[pointerB]
        // and increase both pointerA and pointerB
        // if nums[pointerB] equals to zero, then do nothing about pointerA
        // and increase pointerB
        // make sure pointerA always point to the leftmost zero
        if (nums[pointerB]) {
            pointerA !== pointerB && ([nums[pointerA], nums[pointerB]] = [nums[pointerB], nums[pointerA]]);
            pointerA++;
        }
    }
};
// @lc code=end

Nickname: Geeku From vscode-hzfe-algorithms