leetcode 26 Remove Duplicates from Sorted Array

采用双指针法,慢指针负责更新数值,快指针随着数组元素的遍历同步更新,找到下一个不同的数值进行更新

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        flag = 0 
        for num in  nums:
            if nums[flag] != num:
                flag += 1 
                nums[flag] = num 
        return flag + 1 

你可能感兴趣的:(leetcode 26 Remove Duplicates from Sorted Array)