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].
This problem is required to delete a repeating element in an ordered array.
Because it is an ordered array, the elements of the same value must be in a contiguous position, with the idea of inserting a sort, initially seeing the first element as a non-repeating ordered table, and then sequentially judging that the subsequent element is not the same as the last element of the previous non-repeating ordered array. If the same, then continue to judge backwards, if different, then insert into the previous non-repeating ordered array last until the end of the judgment array.
class Solution {public: intremoveDuplicates(intint n) { if(!n) return0; int i=0,j=1; for(;j<n;j++){ if(A[i]<A[j]){ A[++i]=A[j]; } } return i+1; }};
[Leetcode] Remove Duplicates from Sorted Array