与I类似,增加一个tag变量来判断是否已经出现过两次就可以。
class Solution {
public:
int removeDuplicates(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(n == 0 || n == 1) return n;
int cnt = 1;
bool tag = false;
for(int i = 1; i < n; i++){
if(A[i] == A[i-1]){
if(!tag){
tag = true;
A[cnt++] = A[i];
}
}
else{
tag = false;
A[cnt++] = A[i];
}
}
return cnt;
}
};