Day1--移动零

每天一道Leetcode

class Solution:
    def moveZeroes(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        j = 0   # 记录非零元素应该换到第几个位置
        for i in range(len(nums)):
            if nums[i] != 0:       
                nums[j], nums[i] = nums[i], nums[j]
                j += 1

猜你喜欢

转载自blog.csdn.net/yuzhou_1shu/article/details/82779257
今日推荐