class Solution(object):
def removeElement(self, nums, val):
slow=fast=0
n=len(nums)
while fast<n and slow<n:
if nums[slow]==val:
if nums[fast]!=val:
nums[slow] = nums[fast]
nums[fast]=val
slow+=1
fast+=1
else:
if fast == slow: fast += 1
slow+=1
return slow
又是easy重拳出击的一天。宫水三叶的代码
class Solution(object):
def removeElement(self, nums, val):
slow=0
for fast in nums:
if fast!=val:
nums[slow]=fast
slow+=1
return slow