leetcode-移动零

283. 移动零

题解:

使用双指针法(快慢指针),快指针指向的数字不为0的时候,将这个数字移到慢指针的地方,最后在后面补0即可

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        slow = 0
        for fast in range(len(nums)):
            if nums[fast] != 0:
                nums[slow] = nums[fast]
                slow += 1
        for i in range(slow, len(nums)):
            nums[i] = 0
        return nums

你可能感兴趣的:(leetcode)