Easy
給定有序數(shù)列,去除重復(fù)元素并返回新序列長度。不要建立新序列,保證占用存儲不變。
關(guān)鍵在于不能新建序列,只能在原數(shù)列上刪減。隨著元素刪減,數(shù)列長度會發(fā)生變化,故而倒序刪除更不容易出錯(cuò)。
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)