dmndxld

码不停题

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% 的用户

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

posted on 2019-05-12 12:08  imyourterminal  阅读(185)  评论(0编辑  收藏  举报

导航