码迷,mamicode.com
首页 > 其他好文 > 详细

26. Remove Duplicates from Sorted Array

时间:2020-07-06 16:06:35      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:span   space   ted   odi   elf   rom   lan   mem   dup   

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

 3个指针,一个是当前不同值的位置,另外两个是当前值的左端点和右端点

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n <= 1:
            return n
        index = 0
        l = 0
        r = 1
        while l < n and r < n:
            while r < n and nums[r] == nums[l]:
                r += 1
            nums[index] = nums[l]
            index += 1
            l = r
        return index

 

26. Remove Duplicates from Sorted Array

标签:span   space   ted   odi   elf   rom   lan   mem   dup   

原文地址:https://www.cnblogs.com/whatyouthink/p/13254805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!