class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
# 1.双指针
# l = r = 0
# while r < len(nums):
# if nums[r] != 0:
# nums[l], nums[r] = nums[r], nums[l]
# l += 1
# r += 1
# 2.遍历
index = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[index] = nums[i]
index += 1
for i in range(index, len(nums)):
nums[i] = 0
# 3.遍历
index = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[index], nums[i] = nums[i], nums[index]
index += 1
70-Array--LC283移动零
于 2022-03-16 01:52:01 首次发布