Remove Element
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
"""
#当把长度改变以后,数据再次for loop的时候就乱了,下面的是错误的:
for num in nums:
if num == val:
nums.remove(num)
return len(nums)
"""
start, end = 0, len(nums) - 1
while start <= end:
if nums[start] == val:
nums[start], end = nums[end], end - 1
else:
start +=1
return startLast updated