这道题跟26题的测评方法类似,思路也很简单,就是遍历数组,把与val不相同的数调到前面就行。
下附AC代码:
class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
lens = 0
for i in range(len(nums)):
if val != nums[i]:
nums[lens]=nums[i]
lens+=1
return lens