1. 题目描述
2. 代码
1 class Solution: 2 def moveZeroes(self, nums: 'List[int]') -> None: 3 """ 4 Do not return anything, modify nums in-place instead. 5 """ 6 i,j = 0,0 7 n = len(nums) 8 while i < n: 9 cur = nums[i] 10 if cur == 0: 11 i += 1 12 else: 13 nums[j] = cur 14 i += 1 15 j += 1 16 while j < n: 17 nums[j] = 0 18 j += 1
思路: 双指针, i 用来判断哪一位是0, 若当前位是0, 则 i+1.
若当前位不是0, 则j用来记录不是0的位.
先循环完i, 然后j更新了数组不为0的位, 然后再循环j补足0.