Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 451 Bytes

移动零.md

File metadata and controls

27 lines (22 loc) · 451 Bytes
function moveZeros(nums) {
  const removeElement = (nums, val) => {
    let fast = 0,
      slow = 0;
    while (fast < nums.length) {
      if (nums[fast] != val) {
        nums[slow] = nums[fast];
        slow++;
      }
      fast++;
    }

    return slow;
  };

  let p = removeElement(nums, 0);

  for (; p < nums.length; p++) {
    nums[p] = 0;
  }

  return nums;
}