1 classSolution {2 Public:3 intRemoveDuplicates (intA[],intN) {4 int*s=&a[0],*e=&a[0];//s points at the beginning of the first, e traverses backwards the same5 intt,i,j=N;6 for(i=1; i<n;i++){7e++;8 if(*s==*e)9j--;Ten Else{ Ones++; A*s=*e; - } - } the returnJ; - } -};
Test instructions: Give an ordered array of integers, delete the duplicate elements, leave only one of several identical elements, and return the total number of different elements.
Idea: This is an array, so there is a repetition of the place to move the following elements, with two pointers s and e,e used to traverse all the elements, s to point to the currently processed place, eventually e will traverse to Nth, S will point to the last element of the new array.
Note: There's nothing to be careful of, so short code. Do not cross the pointer.
Leetcode Remove duplicates from Sorted array removes duplicate elements from an integer array and returns the number of remaining elements