Title: Given A sorted array, remove the duplicates in place such, all element appear only once and return the NE W length.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what do you leave beyond the new length
The subject in the leetcode belongs to the Esay level, it is not difficult indeed.
Traverse list, with two variables to record the current position and the value of the next position, if the two values are equal, delete a position element, not equal to two variables are moved backward one bit.
classsolution (object):defremoveduplicates (Self, nums):""": Type Nums:list[int]: Rtype:int""" ifLen (nums) = =0:return0elifLen (nums) = = 1: return1 Current=0 Next= 1 whileTrue:ifNext >=Len (nums): Break ifNums[current] = =Nums[next]: Nums.pop (next)Else: Temp=Next Next= next + 1 Current=TempreturnLen (nums)
"Leetcode" Remove duplicates from Sorted Array