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
相關文章
相關標籤/搜索