Thought:
Use count to trace how many times A[index] has occured.
Code:
public class Solution {
public int removeDuplicates(int[] A) {
int index = 0;
int len = A.length;
int count = 1;
if(A.length <= 1) return len;
for(int i = 1; i < A.length; i++){
if(A[i] == A[index]){
if(count == 2) {
len--;
}else {
count++;
A[++index] = A[i];
}
}else{
A[++index] = A[i];
count = 1;
}
}
return len;
}
}
No comments:
Post a Comment