26. Remove Duplicates from Sorted Array

Posted dmndxld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了26. Remove Duplicates from Sorted Array相关的知识,希望对你有一定的参考价值。

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.

My idea:use 1 pointer to search

 

class Solution:
    def removeDuplicates(self, nums):
        if(nums==[]):
            return 0
        
        a=nums[0]
        p=0
        while(p<len(nums)-1):
            while((nums[p+1]==a)):
                del nums[p+1]
                if(p+1==len(nums)):
                  break

            p=p+1
            if (p == len(nums)):
                break
            a = nums[p]
        return len(nums)
执行用时 : 104 ms, 在Remove Duplicates from Sorted Array的Python3提交中击败了50.59% 的用户
内存消耗 : 14.6 MB, 在Remove Duplicates from Sorted Array的Python3提交中击败了97.03% 的用户

思路跟前面几题差不多其实

以上是关于26. Remove Duplicates from Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章

26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array

#26 Remove Duplicates from Sorted Array

LeetCode OJ 26. Remove Duplicates from Sorted Array

leetcode 26. Remove Duplicates from Sorted Array 80. Remove Duplicates from Sorted Array II

26. Remove Duplicates from Sorted Arrayeasy