leetcode 删除数组中的重复项

leetcode 26.删除排序数组中的重复项

class Solution(object):
    # 重复项保留一个
    def removeDuplicates(self, nums):
        n = len(nums)
        if n<2: return n
        i, j = 0, 1
        while i<=j and j<n:
            if nums[i] == nums[j]:
                j += 1
            else:
                nums[i+1] = nums[j]
                i += 1
                j += 1
        return i+1 

leetcode 27.移除元素

class Solution(object):
    def removeElement(self, nums, val):
        ind = 0
        for i in range(len(nums)):
            if nums[i] != val:
                nums[ind] = nums[i]
                ind += 1
        return ind
上一篇:Python进化算法之多目标优化与代码实战


下一篇:[CF1303F] Number of Components - 并查集,时间倒流