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.
Test instructions: Eliminate all duplicate numbers in the array, repeat numbers only one
It's so easy to use Python for this kind of question.
It's good to eliminate duplicate numbers directly with set set.
1 classSolution (Object):2 def removeduplicates (self, nums):3 """4: Type nums:list[int]5: Rtype:int6 """7L={i forIinchNums}8n=Len (l)9L =list (L)Ten L.sort () Onenums=L A returnN
It's pretty easy to use C.
1 intRemoveDuplicates (int* Nums,intnumssize) {2 intI=0, j=0;3 intflag=int_min;4 for(i=0; i<numssize;i++){5 if(nums[i]!=flag) {6nums[j]=Nums[i];7J + +;8 }9flag=Nums[i];Ten } One returnJ; A}
"Leetcode" 26. Remove Duplicates from Sorted Array