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的第二种解法几乎一模一样,甚至比那个还简单,这里就不再赘述了。
时间: 2024-10-24 17:28:07