public int removeDuplicates(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
if(A == null || A.length == 0){
return 0;
}
if(A.length == 1){
return 1;
}
int dup = A[0];
int length = A.length;
int count = 0;
int start = 1;
while(start < length - count){
if(A[start] == dup){
for(int j = start; j < length - 1 - count; j++){
A[j] = A[j+1];
}
count++;
continue;
}else{
dup = A[start];
start++;
}
}
return length - count;
}Remove Duplicates from Sorted Array
最新推荐文章于 2022-04-06 16:22:53 发布
本文提供了一种使用Java实现的数组去重算法,通过遍历数组并移除重复元素来达到减少数组长度的目的。该算法适用于整型数组,并且考虑了数组为空或只有一个元素的特殊情况。
1106

被折叠的 条评论
为什么被折叠?



