public class Solution {
public int removeDuplicates(int[] A) {
if (A.length<=1) return A.length;
int p=0;
int q=1;
int flag=1;
while (q<A.length) {
if (A[q]==A[p]) {
flag++;
if (flag<=2) {
p++;
A[p] = A[q];
}
} else {
flag=1;
p++;
A[p]=A[q];
}
q++;
}
return p+1;
}
}
1. Similar to Q 24.
回复删除2. Use a flag to count.