mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
增加 283. 移动零 JavaScript版本
This commit is contained in:
@ -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)
|
||||||
|
Reference in New Issue
Block a user