题目
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