法一: class Solution: def removeElement(self, nums: List[int], val: int) -> int: for i in range(len(nums)-1, -1, -1): if(nums[i] == val): nums.pop(i) return len(nums)
法二:
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
ret = 0
for i in nums:
if i != val:
nums[ret] = i
ret += 1
nums[:] = nums[:ret]
return ret