# 移动零

描述

给一个数组 *nums* 写一个函数将 `0` 移动到数组的最后面，非零元素保持原数组的顺序

***

1.必须在原数组上操作\
2.最小化操作数

样例

例1:

```
输入: nums = [0, 1, 0, 3, 12],输出: [1, 3, 12, 0, 0].
```

例2:

```
输入: nums = [0, 0, 0, 3, 1],输出: [3, 1, 0, 0, 0].
```

\
解题思路：

双指针，遇到非零就填入前面数组，最后空余位置0填充。

也可以直接算出来0的数目，nums.count(0).然后前面移动所有非零数字，后面填充0。

````
```python
from typing import (
    List,
)

class Solution:
    """
    @param nums: an integer array
    @return: nothing
    """
    def move_zeroes(self, nums: List[int]):
        # write your code here
        idx = 0
        for i in nums:
            if i:
                nums[idx] = i
                idx += 1
        while idx < len(nums):
            nums[idx] = 0
            idx += 1





```
````


---

# 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://nataliekung.gitbook.io/ladder_code/meta-2024/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.
