26 Remove Duplicates From Sorted Arrays

使用双指针,用一个指针i保存当前没有重复的数组位置下标,另一个j用来进行循环,循环过程中,如果相同则跳过,否则将值赋值到下一个i的位置。注意输入数据的处理。

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        l = len(nums)
        if l == 0:
            return 0
        ans = 0
        for i in range(1,l):
            if nums[ans] == nums[i]:
                continue
            else:
                ans+=1
                nums[ans] = nums[i]
        return ans+1

results matching ""

    No results matching ""