Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
与Remove Duplicates from Sorted Array很雷同!
int removeDuplicates(int A[], int n) {
int current,index;
int count;
current = 1;
index = 0; //取值很关键
count = 1;
if(n <= 2 )return n;
while(current < n)
{
if(A[current] == A[current-1])
{
count++;
if(count <= 2)
{
A[++index]=A[current];//主要在这个地方,小于2的重复,还是的覆盖!
}
current++;
}
else
{
count = 1;
A[++index]=A[current];
current ++;
}
}
return index+1 ;
}