Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 648 Bytes

27.md

File metadata and controls

29 lines (21 loc) · 648 Bytes

27. Remove Element

给定一个数组,删除数组中一个特定的数字,该数字可能出现多次,返回新的数组的长度

思路: 从头开始,每当有一个值不等于val, 将其放在数组对应索引上,最后nums[0:j]都是不等于val的数值了

Python:

class Solution:
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        # 5 star
        j = 0
        for i in range(len(nums)):
            if nums[i] != val:
                nums[j] = nums[i]
                j += 1
        return j

todo, go