8.移动零(Python)

版权声明:@author:geek_aaron https://blog.csdn.net/weixin_39433783/article/details/83010359

在这里插入图片描述
注意理解remove和del的区别!!!
(1)remove删除的是首次出现在列表中的元素.
(2)del删除的就是那个索引的元素.

参考代码

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

猜你喜欢

转载自blog.csdn.net/weixin_39433783/article/details/83010359