> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/201-400/283.-yi-dong-ling.md).

# 283. 移动零

<https://leetcode-cn.com/problems/move-zeroes/submissions/>

## 解法一：

逆向思维，将所有非0元素挪到数组前部分即可。以i为界划分非0和0，下标i之前的元素均为非0。用一个游标j一次遍历数组，遇到非0就将其和i处元素交换。

```javascript
/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var moveZeroes = function(nums) {
    let i = 0
    for (let j in nums) {
        if (nums[j]) {  //若非0，i与j位置元素交换，非0区i向前一位
            [nums[i], nums[j]] = [nums[j], nums[i]]
            i++
        }
    }
};
```
