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)