27. Remove Element(列表)

题目

在这里插入图片描述

代码

class Solution:
    def removeElement(self, nums, val):
        start, end = 0, len(nums) - 1
        while start <= end:
            if nums[start] == val:
                nums[start], nums[end], end = nums[end], nums[start], end - 1
            else:
                start +=1
        return start 
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        i = 0
        p = 0 #last element that
        length = 0
        while i < len(nums):
            if nums[i] == val:
                pass
            else:
                nums[p] = nums[i]
                p += 1
                length += 1
            i += 1
        return length

猜你喜欢

转载自blog.csdn.net/xiabenshu/article/details/89062482