Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
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 are you leave beyond the new length.
The subject requires the In-place in the sroted list to be re-operated, and returns the length of the weight.
Idea: Assuming that after a few steps, the nums is already a sequence of go-to-finish. Set the IND as the first index after the de-sequencing. Look back from IND, if nums[i]! = prev, then nums[ind] = Nums[i], and then go to the end of the re-completed sub-sequence edge long a grid, so ind + = 1. This prev is always located in the previous one of the IND, so it is not possible to define it separately with nums[ind-1].
1, 2, 3, 4, 4, 5, 6, 7, 7, ...
Prev, Ind
1 classsolution (object):2 defremoveduplicates (Self, nums):3 """4 : Type Nums:list[int]5 : Rtype:int6 """7n =Len (nums)8 ifN <= 1:9 returnNTen OneIND = 1 A forIinchRange (1, N): - ifNums[i]! = nums[ind-1]: -Nums[ind] =Nums[i] theIND + = 1 - returnInd
Leetcode 26. Remove Duplicates from Sorted Array