#283. Move Zeroes

https://leetcode.com/problems/move-zeroes/#/description

#283. Move Zeroes_第1张图片
image.png

Python

  • 遇到非零元素则替换到前面去
  • flag 为前面非零元素个数
class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        flag = 0
        for i in range(len(nums)):
            if nums[i] != 0:
                nums[i], nums[flag] = nums[flag], nums[i]
                flag += 1

你可能感兴趣的:(#283. Move Zeroes)