Title Description
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 A = [1,1,2],
Your function should return length = 2, and A is now [all].
Thinking of solving problems
The topic requires removing the extra elements in the sorted array, changing the array and returning the number of new arrays. That is, the original array is a = [1,1,2], the result of processing should be a = [1,2,*], the different element 2 is placed after 1, and the length of the returned array is 2.
The method can be implemented by using two array ordinal index or pointer. The first index points to the last valid element, the second index iterates backwards, and if the current traversal element is different from the index element, the index is shifted one bit, and the element that the second index points to is paid to the element of the first index. Finally, return to the position of index. Time of the time of the MS, relatively slow ~ do not know if you have a better idea, welcome message ~ ~ ~
class solution { public : int removeduplicates (int a[], int N) {int count = 0 ; for (int i = 1 ; i < n; i++) {if (a[i] = = A[i-1 ]) count + +; else A[i-count ] = a[i]; } return n-count ; }};
Leetcode (6) Remove duplicates from Sorted Array