Original link: leetcode-cn.com/problems/mo…

Reference problem solved animation demonstration 283. Moving zero one – time traversal solution.

Answer:

  1. Fast pointer traversal number group. The slow pointer indicates where non-zero elements are stored
  2. When the fast pointer traverses a non-zero element, it moves to the slow pointer position and increments the slow pointer by one

Time complexity: O(n) Space complexity: O(1)

/** * @param {number[]} numbers * @return {void} Do not return anything, */ var moveZeroes = function (numbers) {// If (numbers) { Let moveIndex = 0; let moveIndex = 0; Numbers. ForEach ((value, index) => {// If (value! If (moveIndex! Numbers [moveIndex] = value; Numbers [index] = 0; } // after the move, move the pointer to the next position to move moveIndex++; }}); };Copy the code