WillWang-X
7/18/2017 - 2:23 AM

26 Remove Duplicates from Sorted Array.py

# Time: O(n)
# Space: O(1)
# Remove Dupicates from sorted Array 
'''
 0 1 2 3 4
[1,1,2,2,3]

last = 0
i = 1
'''

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