[LeetCode] 80. Remove Duplicates from Sorted Array II

夜歌乘年少發表於2024-06-30

原來leetcode使用Count也不需要import collections

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        # len =0
        if len(nums) == 0:
            return 0
        # else
        countList = Counter(nums)
        countModify = {key:min(value,2) for key,value in countList.items()}
        ret = [key for key,value in countModify.items() for _ in range(value)]
        nums[:] = ret
        return len(nums)

image

相關文章