Simple and O(n) Python


  • 0
    C

    class Solution(object):

    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0
        if len(nums)==1:
            return 1
        pos = 1
        now = nums[0]
        for cursor in xrange(1,len(nums)):
            if nums[cursor]==now:
                continue
            nums[pos],nums[cursor]=nums[cursor],nums[pos]
            now = nums[pos]
            pos+=1
            
        return pos

  • 0
    Z

    But duplicate elements wasn't removed from the original array(list).
    The requirement of this problem can't be satisfied completely in python because python doesn't allow delete elements when looping over a list!


Log in to reply
 

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