a python solution: not the shortest but easy to understand


  • 0
    M
    class Solution(object):
        def removeDuplicates(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            # why the heck LC thinks corner case [] here is meaningful!!!
            if nums == []:
                return 0
            
            duplicate = 0
            max_num = max(nums)
            for i in range(len(nums)-1):
                if nums[i] == nums[i+1]:
                    duplicate += 1
                    nums[i] = max_num
            nums.sort()
            
            nums = nums[0:len(nums)-duplicate]
            
            return len(nums)
            
    

Log in to reply
 

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