移动零python3(leetcode283)

#283. 移动零

#给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

class Solution(object):
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        j = 0
        for i in range(0, len(nums)):
            if nums[i] != 0:
                nums[j] = nums[i]
                j +=1
        for i in range(j, len(nums)):
            nums[i] = 0 
        return nums

猜你喜欢

转载自blog.csdn.net/ziqingnian/article/details/121791723