27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

AC代码:

class Solution(object):
    def removeElement(self, nums, val):
        current = 0
        for v in nums:
            if v != val:
                nums[current] = v
                current += 1
        return current

本题和26. Remove Duplicates from Sorted Array的第二种解法几乎一模一样,甚至比那个还简单,这里就不再赘述了。

原文地址:https://www.cnblogs.com/zhuifengjingling/p/5246125.html