Leetcode 80. Remove Duplicates from Sorted Array II

Leetcode 26的增强版,只须要多加一个变量记录重复次数.html

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        size=len(nums)
        if size in [0,1,2]:
            return size
        i=0
        n=1
        for j in range(1,size):
            if nums[j]==nums[i]:
                if n==1:
                    i+=1
                    nums[i]=nums[j]
                n+=1
            elif nums[j]!=nums[i]:
                i+=1
                nums[i]=nums[j]
                n=1
        return i+1
相关文章
相关标签/搜索