首页 > 学院 > 开发设计 > 正文

leetcode 26. Remove Duplicates from Sorted Array

2019-11-08 01:20:24
字体:
来源:转载
供稿:网友
题目:Total Accepted: 199594Total Submissions: 563740Difficulty: EasyContributors: Admi

Given a sorted array, 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 in place with constant memory.

思路:使用index记住当前位置,从下标1开始遍历list,如果重复则替换index+1的位置。返回index+1.

class Solution(object):    def removeDuplicates(self, nums):        """        :type nums: List[int]        :rtype: int        """        if len(nums)==0:return 0        ind=0;i=1        while i<len(nums):            if nums[ind]!=nums[i]:                ind+=1;nums[ind]=nums[i]            i+=1        return ind+1


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表