Remove Duplicates from Sorted Array |
代码
class Solution {
public:
int removeDuplicates(int A[], int n) {
int cnt = 0;
if(n==0)
return 0;
cnt++;
for(int i = 1; i < n; i++)
{
if(A[i]!=A[i-1])
{
A[cnt] = A[i];
cnt++;
}
}
return cnt;
}
};
本文介绍了一种从有序数组中去除重复元素的方法。通过一个简单的C++类实现,该算法遍历数组并将不重复的元素保留下来,最终返回处理后数组的有效长度。这种方法在保证数组元素顺序的同时,提高了存储效率。
1092

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



