[LeetCode] 80. Remove Duplicates from Sorted Array II

原来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

posted @ 2024-06-30 12:58  夜歌乘年少  阅读(2)  评论(0编辑  收藏  举报