Topic
Given a sorted array, remove the duplicates in place such, all element appear only once and return the new 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 1 and 2 respectively. It doesn ' t matter what are you leave beyond the new length.
Analysis
Such a simple topic, unexpectedly 3 times only AC. It's a failure!
Summary, is only to find out the number of non-repeating elements, but ignore the input parameter is a reference, you must also make the array is stored as non-repeating elements. That is, the number must be updated while the elements in the array cannot be duplicated.
AC Code
classSolution { Public:intRemoveDuplicates ( vector<int>& Nums) {if(Nums.size () <=1)returnNums.size ();inti =0, j =1; while(J < Nums.size ()) {if(Nums[i] = = Nums[j]) {++j; }Else{Nums[++i] = nums[j++]; } }returni +1; }};
GitHub test Program source code
Leetcode (+) Remove duplicates from Sorted Array