From ff3f31aed05dd05c5740b63415f494b198c8a477 Mon Sep 17 00:00:00 2001 From: jerryfishcode <91447694+jerryfishcode@users.noreply.github.com> Date: Mon, 27 Sep 2021 16:14:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20283.=20=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E9=9B=B6=20JavaScript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0283.移动零.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 3909bcd5..2498bdc3 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -95,6 +95,21 @@ Python: Go: JavaScript: +```javascript +var moveZeroes = function(nums) { + let slow = 0; + for(let fast = 0; fast < nums.length; fast++){ + if(nums[fast] != 0){//找到非0的元素 + nums[slow] = nums[fast];//把非0的元素赋值给数组慢指针指向的索引处的值 + slow++;//慢指针向右移动 + } + } + // 后面的元素全变成 0 + for(let j = slow; j < nums.length; j++){ + nums[j] = 0; + } +}; +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)