539 · 移动零

描述
给一个数组 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].

class Solution:
    """
    @param nums: an integer array
    @return: nothing
    """
    def moveZeroes(self, nums):
        i, j = 0,0
        while i<len(nums):
            if nums[i] == 0:
                i += 1
                continue
            nums[j] = nums[i]
            i += 1
            j += 1
        for i in range(j,len(nums)):
            nums[i] = 0
        return 
原文地址:https://www.cnblogs.com/bernieloveslife/p/14638691.html