LeetCode 283 Move Zeroes (Python)

Posted by 小明MaxMing on April 26, 2020

题目

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

解题思路

代码

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        l, r, n = 0, 0, len(nums)
        while r < n:
            if nums[r] != 0:
                nums[l] = nums[r]
                l += 1
            r += 1
        while l < n:
            nums[l] = 0
            l += 1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅