美文网首页
Remove Duplicates from Sorted Ar

Remove Duplicates from Sorted Ar

作者: 穿越那片海 | 来源:发表于2017-03-05 19:48 被阅读0次

Easy

给定有序数列,去除重复元素并返回新序列长度。不要建立新序列,保证占用存储不变。

关键在于不能新建序列,只能在原数列上删减。随着元素删减,数列长度会发生变化,故而倒序删除更不容易出错。

class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        for i in xrange(len(nums)-1,0,-1):
            if nums[i] == nums[i-1]:
                nums.pop(i)

相关文章

网友评论

      本文标题:Remove Duplicates from Sorted Ar

      本文链接:https://www.haomeiwen.com/subject/nupegttx.html