Remove Duplicates from Sorted Ar

2017-03-05  本文已影响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)
上一篇 下一篇

猜你喜欢

热点阅读