Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 986 Bytes

26 remove duplicates.md

File metadata and controls

35 lines (27 loc) · 986 Bytes

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

要求是o(1)的复杂度,可以替换前面输入进来的list

class Solution:
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums)==0:
            return 0
        if len(nums)==1:
            return 1
        z=0
        for i in range(len(nums)):
            if nums[z]!=nums[i]:
                z=z+1
                nums[z]=nums[i]
        return z+1