水题 需要一个counter来记录出现了几次。
update the count first and the count will be condition to store the value.
public int removeDuplicates(int[] A) {
if (A.length < 3) return A.length;
int index = 1;
int count = 1;
for (int i = 1; i < A.length; i++){
if (A[i] == A[i-1]){
count++;
} else {
count = 1;
}
if (count <= 2){
A[index] = A[i];
index++;
}
}
return index;
}
No comments:
Post a Comment