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
posted @ 2020-05-10 18:00  YTiing  阅读(222)  评论(0编辑  收藏  举报