Simple Python Code O(n) top 0.7%


  • 0
    C

    Since it's sorted array, each time we find a new element, we move it to its place:

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.