Python solution using dictionary (Looking for better solutions)


  • -2
    C

    Here's my solution

    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def removeDuplicates(self, nums):
        	d = dict()
        	i = 0
        	while i < len(nums):
        		if nums[i] in d:
        			nums.pop(i)
        			#after pop, i does not change since len(nums) changed
        		else:
        			d[nums[i]] = nums[i]
        			i += 1
    
        	return len(nums)

  • 0
    C
    class Solution:
        # @param {integer[]} nums
        # @return {integer}
        def removeDuplicates(self, nums):
            last=None
            rt=0
            for x in nums:
                if x!=last:
                    last=x
                    nums[rt]=x
                    rt+=1
            return rt
    

    Here's a better one.


Log in to reply
 

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