Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
注意一点:数组长度减1之前,要将整个数组向左平移
代码:
public int removeDuplicates(int[] A) {
int size = A.length;
int i = 1;
while(i < size){
if(A[i] == A[i-1]){
//数组整体左移
for(int j = i+1;j<size;j++){
A[j-1] = A[j];
}
size--;
}else{
i++;
}
}
return size;
}