https://leetcode-cn.com/problems/remove-element/
用i标记无val区的尾部,即[0:i]是无val的区域;j一次遍历数组,遇到不等于val的就赋给i处,然后i后移
[0:i]
class Solution: def removeElement(self, nums: List[int], val: int) -> int: i = j = 0 while j < len(nums): if nums[j] != val: nums[i] = nums[j] #无val区扩张 i += 1 j += 1 return i #i即为无val区的长度
最后更新于5年前