LeetCode_Python3: 27. 移除元素(简单)

开始之前:从2018/8/27开始刷LeetCode,计划每周刷五题,周末进行总结并发布在csdn上,计划先刷150道题,从简单开始。

week2-4


要求:

CODE:

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        # 利用python的del来做十分简单
        # 注意从末位开始索引,这样不会搞乱顺序
        
        for i in range(len(nums)-1,-1,-1):
            if nums[i] == val:
                del nums[i]
        return len(nums)

结果:

知识点:

猜你喜欢

转载自blog.csdn.net/Kuroyukineko/article/details/82529498