O(n) Python solution (while loop)


  • 0
    A
    class Solution(object):
        def removeDuplicates(self, nums):
            n=len(nums)
            if not nums:
                return 0
            i=0
            j=1
            while j<n:
                if nums[i]!=nums[j]:
                    nums[i+1],nums[j]=nums[j],nums[i+1]
                    j+=1
                    i+=1
                else: j+=1
            return i+1

Log in to reply
 

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