边工作边刷题:70天一遍leetcode: day 23

Remove Duplicates from Sorted Array II

错误点

  • 不能swap,而是要只把后面的元素取代前面的。swap将破坏nums[i]!=nums[i-1] check
class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums: return 0
        count = False
        j = 0
        for i in range(1, len(nums)):
            if nums[i]!=nums[i-1]:
                count = False
                j+=1
                nums[j] = nums[i]
            elif not count:
                count = True
                j+=1
                nums[j] = nums[i]
        
        return j+1
posted @ 2016-04-26 10:17  absolute100  阅读(94)  评论(0编辑  收藏  举报