首页 > 其他 > 详细

26. Remove Duplicates from Sorted Array

时间:2019-05-12 12:34:33      阅读:99      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/dmndxld/p/10851788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!