leetcode283:移动零

思想:

定义变量i控制遍历列表nums,变量控制j控制0元素下标移动。判断nums[i]是否等于0,若是则将nums[i]和nums[j]调换位置并且j+1。反之继续遍历。

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

这都是大佬的思想,我个小菜鸟

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/83472388