Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

移动零-283 #26

Open
sl1673495 opened this issue May 11, 2020 · 0 comments
Open

移动零-283 #26

sl1673495 opened this issue May 11, 2020 · 0 comments
Labels
双指针 待复习 看题解或者做出来很艰难的,需要回顾。

Comments

@sl1673495
Copy link
Owner

sl1673495 commented May 11, 2020

  1. 移动零

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

必须在原数组上操作,不能拷贝额外的数组。尽量减少操作次数。
https://leetcode-cn.com/problems/move-zeroes

思路

暴力法

先遍历一次,找出所有 0 的下标,然后删除掉所有 0 元素,再 push 相应的 0 的个数到
末尾。

var moveZeroes = function (nums) {
  let zeros = []
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] === 0) {
      zeros.push(i)
    }
  }
  for (let j = zeros.length - 1; j >= 0; j--) {
    nums.splice(zeros[j], 1)
  }
  for (let j = 0; j < zeros.length; j++) {
    nums.push(0)
  }
  return nums
}

双指针

慢指针 j 从 0 开始,当快指针 i 遍历到非 0 元素的时候,i 和 j 位置的元素交换,然
后把 j + 1。

也就是说,快指针 i 遍历完毕后, [0, j) 区间就存放着所有非 0 元素,而剩余的[j,
n]区间再遍历一次,用 0 填充满即可。

gif

var moveZeroes = function (nums) {
  let j = 0

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) {
      nums[j] = nums[i]
      j++
    }
  }

  while (j < nums.length) {
    nums[j] = 0
    j++
  }
}

双指针(交换位置)

在上面的算法里,快指针遍历完成后,还要遍历慢指针到末尾来填充 0。实际上这题只要遇
到非 0 元素,就把当前位置的值和慢指针位置 j 的值交换,然后只有此时 j 才 + 1,即
可完成。

gif

var moveZeroes = function (nums) {
  let j = 0

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) {
      swap(nums, i, j)
      j++
    }
  }
}

function swap(nums, i, j) {
  let temp = nums[i]
  nums[i] = nums[j]
  nums[j] = temp
}
@sl1673495 sl1673495 added 双指针 待复习 看题解或者做出来很艰难的,需要回顾。 labels May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
双指针 待复习 看题解或者做出来很艰难的,需要回顾。
Projects
None yet
Development

No branches or pull requests

1 participant