public class Solution {
public int removeDuplicates(int[] A) {
int count = 0;
if (A == null || A.length == 0) {
return count;
}
count = 1;
// 从索引1开始遍历,获得不重复个数
for (int i = 1; i < A.length; i++) {
if (A[i] != A[i - 1]) {
count ++;
}
}
int uniqueIndex = 1; // 记录已经有了多少个不重复的数字被换到了前面
for (int i = 1; i < A.length; i++) {
if (A[i] != A[i-1]) {
A[uniqueIndex] = A[i];
uniqueIndex++;
}
}
return count;
}
}
网友评论