linearList02:Remove Duplicates from Sorted Array II
描述:
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]
解决方法:
// 2015.7.3
#include <iostream>
using namespace std;
int removeDuplicates(int A[],int n);
void main()
{
int A[6]={1,1,1,2,2,3};
cout<<"长度是"<<removeDuplicates(A,6)<<endl;
}
int removeDuplicates(int A[],int n)
{
if(n<=2) return 0;
int index=2;
for(int i=2;i<n;i++)
{
if(A[index-2]!=A[i])
{
A[index++]=A[i];
}
}
for(int j=0;j<index;j++)
{
cout<<A[j]<<endl;
}
return index;
}
转载于:https://blog.51cto.com/10487836/1670427