leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

时间:2023-03-09 05:04:29
leetcode 题解: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].

说明:

1)设个标志可实现

实现:

 class Solution {
public:
int removeDuplicates(int A[], int n) {
if(==n) return ;
int B[n],k=,flag=;
for(int i=;i<n;i++)
B[i]=;
B[]=A[];
for(int i=;i<n;i++)
{
if(B[k]==A[i])
flag++;
else
flag=;
if(flag<)
B[++k]=A[i];
}
for(int i=;i<=k;i++)
A[i]=B[i];
return k+;
}
};