增加 283. 移动零 JavaScript版本

This commit is contained in:
jerryfishcode
2021-09-27 16:14:35 +08:00
committed by GitHub
parent 55e185b5e6
commit ff3f31aed0

View File

@ -95,6 +95,21 @@ Python
Go Go
JavaScript 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) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)