[Leetcode] 27. 移除元素 python3

和26题十分相似。

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        l = len(nums)
        i = 0
        for count in range(l): #count用于累计比较次数
            if nums[i] != val:#不等于val
                i += 1
                count += 1
            else:                   #等于val
                del nums[i]       #删除
                count += 1
        return len(nums)

猜你喜欢

转载自blog.csdn.net/niceHou666/article/details/81276215
今日推荐