https://leetcode.com/problems/move-zeroes/#/description
image.png
Python
- 遇到非零元素则替换到前面去
- flag 为前面非零元素个数
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
flag = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[i], nums[flag] = nums[flag], nums[i]
flag += 1