题目:
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]
.
代码如下:
int removeDuplicates(int A[], int n) {
if(n<=2)return n;
int j=0,count=0;
for(int i=1;i<n;i++)
{
if(A[i]!=A[j])
{
A[++j]=A[i];
count=0;
}
else
{
if(count==0)
{
A[++j]=A[i];
count++;
}
}
}
return j+1;
}