扫描数组,用一个指针维护去重后的的数组。 这样在不需要额外数组空间的情况下可以获得去重数组的长度并且排列在A的前端。
public class Solution {
public int removeDuplicates(int[] A) {
if(A==null||A.length==0)
{
return 0;
}
int len = A.length;
int res=1;
for( int i=1;i<len;i++ )
{
if(A[i]==A[res-1])
{
continue;
}
A[res]=A[i];
res++;
}
return res;
}
}