您的位置:首页 > 其它

leetcode_linearList02

2015-07-03 09:48 1351 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode