# 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++
        }
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cai-sen-se.gitbook.io/leetcode/201-400/283.-yi-dong-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
