Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 552 Bytes

70. 移动零.md

File metadata and controls

18 lines (16 loc) · 552 Bytes

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。即输入: [0,1,0,3,12] 输出: [1,3,12,0,0]

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """

        n = len(nums)
        i = 0
        for j in range(n):
            if nums[j]:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1
        return nums