Skip to content

第 82 题:算法题「移动零」,给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 #132

@yygmind

Description

@yygmind
Contributor

示例:

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

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。

  2. 尽量减少操作次数。

Activity

ChuYang-FE

ChuYang-FE commented on May 27, 2019

@ChuYang-FE

新增:解决了有连续的0无法实现功能的问题。

function zeroMove(array) {
        let len = array.length;
        let j = 0;
        for(let i=0;i<len-j;i++){
            if(array[i]===0){
                array.push(0);
                array.splice(i,1);
                i --;
                j ++;
            }
        }
        return array;
    }
Rashomon511

Rashomon511 commented on May 27, 2019

@Rashomon511
        const moveZore = (arr) => {
            let n = 0
            arr.forEach((item, index) => {
                if (item === 0){
                    arr.splice(index, 1)
                    n++;
                }
            })
            arr.push(...(new Array(n)).fill(0))
            return arr;
        }
y1324

y1324 commented on May 27, 2019

@y1324

let nums = [0,1,0,3,12];

for (let i = 0; i < nums.length; i++){
  if (nums[i] === 0){
    nums.push(0);
    nums.splice(i,1);
  }
};
fengjinlong

fengjinlong commented on May 27, 2019

@fengjinlong

let arr = [1,2,3,0,7,0]
j = arr.length
for (i = 0; i < j; i++) {
if (arr[i] === 0) {
arr.splice(i,1)
arr.push(0)
}
}

btea

btea commented on May 27, 2019

@btea
function move(arr){ 
  var n = 0; 
  for(var i = 0; i < arr.length; i++){ 
    if(arr[i] === 0){ 
       arr.splice(i, 1);
       n++; 
       i--; 
    }
  } 
  while(n--){ 
    arr.push(0); 
  }
  return arr;
}
wingmeng

wingmeng commented on May 27, 2019

@wingmeng

已修正连续多个 0 时的 bug
思路:用一个 len 记录 0 的出现次数,再过滤掉原数组中所有的 0,最后在数组末尾补上被移除的 0

let nums = [0, 0, 0, 1, 0, 3, 12];

console.log(moveZeroToLast(nums));  // [1, 3, 12, 0, 0, 0, 0]

function moveZeroToLast(arr) {
  let len = arr.length;

  return arr.filter(it => it === 0 ? len-- && false : true)
    .concat(Array(arr.length - len).fill(0));
}
jjeejj

jjeejj commented on May 27, 2019

@jjeejj
Contributor

上面所有的再循环中,先 splice 在 push 的方法都是有问题的

因为,当splice 一个元素的时候,紧跟着的后面一个元素会向前移动一位,索引会变成正在删除那个元素的,所有当有连续的 0 时候,无法满足要求

wz71014q

wz71014q commented on May 27, 2019

@wz71014q
const array = [2, 0, 1, 4, 0, 0, 0, 5, 7, 8];
array.sort((fir, sec) => {
  return fir === 0;
})
// array = [ 2, 1, 4, 5, 7, 8, 0, 0, 0, 0 ]

这里有个问题,首元素为0无效,不知道为什么,求教

const array = [0, 2, 0, 1, 4, 0, 0, 0, 5, 7, 8];
array.sort((fir, sec) => {
  return fir === 0;
})
console.log(array);
// array = [ 8, 7, 1, 4, 2, 5, 0, 0, 0, 0, 0 ]
Hunterang

Hunterang commented on May 27, 2019

@Hunterang

let moveZero = (arr) => {
let point = 0
for (var i = 0; i < arr.length; i++) {
if (arr[i]!=0) {
arr[point] = arr[i]
arr[i] = 0
point++
}
}

return arr
}

tanggd

tanggd commented on May 27, 2019

@tanggd

最优解法

想到好几个方法,楼上都写出来了,但是我觉得这些都不是最优解法,既然是算法题,自然是以算法的思维去解。

function moveZeroToLast(arr) {
    let index = 0;
    for (let i = 0, length = arr.length; i < length; i++) {
        if (arr[i] === 0) {
            index++;
        } else if (index !== 0) {
            arr[i - index] = arr[i];
            arr[i] = 0;
        }
    }
    return arr;
}
y1324

y1324 commented on May 27, 2019

@y1324

上面所有的再循环中,先splice在推的方法都是有问题的

因为,当splice一个元素的时候,紧跟着的后面一个元素会向前移动一位,索引会变成正在删除那个元素的,所有当有连续的​​0时候,无法满足要求

那个这个问题怎么解决?没头绪

wheatma

wheatma commented on May 27, 2019

@wheatma
const moveZeroToEnd = (arr) => {
    let index = 0
    let current = 0
    while(index < arr.length) {
        if (arr[current] === 0) {
            arr.splice(current, 1)
            arr.push(0)
        } else {
            current++
        }
        index++
    }
    return arr
}
moorain

moorain commented on May 27, 2019

@moorain
//倒序循环可避免
const arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 9, 9, 9, 0, 0, 0, 0, 1, 0, 3, 12, 0, 0, 0, 0];
    const len = arr.length;
    console.log(len)
    for (let i = len; i >= 0; i--) {
      if (arr[i] === 0) {
        arr.splice(i, 1);
        arr.push(0)
      }
    }
    console.log(arr)
y1324

y1324 commented on May 27, 2019

@y1324
//倒序循环可避免
const arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 9, 9, 9, 0, 0, 0, 0, 1, 0, 3, 12, 0, 0, 0, 0];
    const len = arr.length;
    console.log(len)
    for (let i = len; i >= 0; i--) {
      if (arr[i] === 0) {
        arr.splice(i, 1);
        arr.push(0)
      }
    }
    console.log(arr)

为什么倒序可以解决这个办法

moorain

moorain commented on May 27, 2019

@moorain
//倒序循环可避免
const arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 9, 9, 9, 0, 0, 0, 0, 1, 0, 3, 12, 0, 0, 0, 0];
    const len = arr.length;
    console.log(len)
    for (let i = len; i >= 0; i--) {
      if (arr[i] === 0) {
        arr.splice(i, 1);
        arr.push(0)
      }
    }
    console.log(arr)

为什么倒序可以解决这个办法

因为待处理的数据索引没有变。https://www.jianshu.com/p/77247e9e1849

225 remaining items

qifengla

qifengla commented on May 31, 2023

@qifengla
function moveZeroes(nums) {
  const len = nums.length;
  let left = 0;
  
  for (let right = 0; right < len; right++) {
    if (nums[right] !== 0) {
      nums[left] = nums[right];
      left++;
    }
  }
  
  for (let i = left; i < len; i++) {
    nums[i] = 0;
  }
}

const nums = [0, 1, 0, 3, 12];
moveZeroes(nums);
console.log(nums);
shizhouyu

shizhouyu commented on May 31, 2023

@shizhouyu
Stephen-Monster

Stephen-Monster commented on May 31, 2023

@Stephen-Monster
88wixi

88wixi commented on Jul 24, 2023

@88wixi

let arr = [0, 1, 0, 3, 12, 0, 9, 8, 6, 0, 1]
function changeArea(arr) {
arr.forEach((element, index) => {
if (element === 0) {
for (i = index; i < arr.length - 1; i++) {
let temp
temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
}
}
});
}
changeArea(arr)
console.log(arr)

shizhouyu

shizhouyu commented on Jul 24, 2023

@shizhouyu
Stephen-Monster

Stephen-Monster commented on Jul 24, 2023

@Stephen-Monster
wut1

wut1 commented on Aug 12, 2023

@wut1

let len = nums.length
let i = 0
let max = len
while(i < max){
if(nums[i] === 0){
nums.splice(i,1)
nums.push(0)
max--
} else {
i++
}

}
return nums

shizhouyu

shizhouyu commented on Aug 12, 2023

@shizhouyu
Stephen-Monster

Stephen-Monster commented on Aug 12, 2023

@Stephen-Monster
shizhouyu

shizhouyu commented on Nov 3, 2023

@shizhouyu
Stephen-Monster

Stephen-Monster commented on Nov 3, 2023

@Stephen-Monster
xiguahuibaozha

xiguahuibaozha commented on Nov 3, 2023

@xiguahuibaozha

[0, 1, 0, 3, 12, 0, 9, 8, 6, 0, 1].sort((a,b) => { if(b){ return 0 }else { return -1 } })

MyPrototypeWhat

MyPrototypeWhat commented on Jun 3, 2024

@MyPrototypeWhat

一个循环就结束 很简单

const fn = (arr) => {
  let count = 0;
  for (let i in arr) {
    if (arr[i] === 0) {
      count++;
    } else {
      arr[i - count] = arr[i];
      arr[i] = 0;
    }
  }
};
shizhouyu

shizhouyu commented on Jun 3, 2024

@shizhouyu
Stephen-Monster

Stephen-Monster commented on Jun 3, 2024

@Stephen-Monster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @benzhemin@ysyfff@xiaotaotao2035@towersxu@crabkiller

        Issue actions

          第 82 题:算法题「移动零」,给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 · Issue #132 · Advanced-Frontend/Daily-Interview-Question