Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place and return the new length.

class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        c= 1
        """
        此題重點是要把數字往前搬(不是swap, 是直接assign) 然後用一個指標專門在前頭導引插入位置
         [1, 1, 1, 1, 1, 2, 3, 3]
             2  3
                   c
                            i
        """
        for i in range(1, len(nums)):
            if nums[i-1] != nums[i]:
                nums[c] = nums[i]
                c = c + 1
        return c

Last updated