lc283. 移动零

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        i, j = 0, 0
        while j < len(nums):
            if nums[j] != 0 and i == j:
                i += 1
            elif nums[j] != 0 and i != j:
                nums[i] = nums[j]
                i += 1
            j += 1
        # i后面全补0(包括i)
        while i < len(nums):
            nums[i] = 0
            i += 1

 

posted @ 2022-04-30 18:06  stronger_el  阅读(22)  评论(0)    收藏  举报