Scribbling

LeetCode: 80. Remove Duplicates from Sorted Array II 본문

Computer Science/Coding Test

LeetCode: 80. Remove Duplicates from Sorted Array II

focalpoint 2021. 10. 6. 14:26
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        reg, freq = None, None
        k = 0
        for i, num in enumerate(nums):
            # update reg & freq
            if reg != num:
                reg = num
                freq = 1
            else:
                freq += 1
            if freq <= 2:
                nums[k] = num
                k += 1
        return k