leetcode_27. 移除元素python

算法思想:

这题要比上一个26. 删除排序数组中的重复项简单一些,具体思想参考上一篇,还是使用的enumerate函数。

代码:

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        pre_index = 0
        for index, num in enumerate(nums):
            if num == val:
                continue
            else:
                nums[pre_index] = num
                pre_index += 1
        return pre_index

猜你喜欢

转载自blog.csdn.net/qq_37002901/article/details/87912626