> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/meta-2024/yi-dong-ling.md).

# 移动零

https\://www\.lintcode.com/problem/539/description?utm\_source=sc-libao-ql

描述

给一个数组 *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





```
````
