算法思想:
这题要比上一个26. 删除排序数组中的重复项简单一些,具体思想参考上一篇,还是使用的enumerate函数。
代码:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if len(nums) == 0:
return 0
pre_index = 0
for index, num in enumerate(nums):
if num == val:
continue
else:
nums[pre_index] = num
pre_index += 1
return pre_index