Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, given input array A = [1,1,2], Your function should return length = 2, and A is now [1,2].
相关问题1:压缩字符串
相关问题2:Remove Duplicates from Sorted Linked List 从排序链表中去掉重复值
相关问题3:[LeetCode] Remove Duplicates From Sorted Array II
代码:
int removeDuplicates(int A[], int n) {
if(n==1) return 1;
if(n==0) return 0;
int i=0, j=1;
int count = 1;
for(j=1; j<n; j++)
{
if(A[j]!=A[j-1])
{
A[i]=A[j-1];
i++;
}
}
A[i] = A[j-1];
i++;
return i;
}