leetcode 每日一题 27. 移除元素
双指针法
思路:
class Solution: def removeElement(self, nums: List[int], val: int) -> int: n = 0 for j in range(len(nums)): if nums[j] != val: nums[n] = nums[j] n += 1 return n
python可用方法
class Solution: def removeElement(self, nums: List[int], val: int) -> int: while val in nums: nums.remove(val) return len(nums)