从排序好的列表中删除重复元素
Given input array A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
java 版本如下
public static int removeDuplicates(int[] A) {
if(A == null || A.length == 0) return 0;
int pre = 0,cur = 1, pos = 1;
while(cur < A.length){
if(A[cur] != A[pre]){
A[pos] = A[cur];
pos++;
}
pre++;
cur++;
}
return pos;
}