class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(0 == n) return 0;
int len = 1;
for (int i = 1; i < n; ++i)
{
if(A[i] != A[len-1])
A[len++] = A[i];
}
return len;
}
};
second time
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n == 0) return 0;
int newLen = 1;
for(int i = 1; i < n; ++i)
{
if(A[i] != A[newLen-1])
{
A[newLen] = A[i];
newLen++;
}
}
return newLen;
}
};
本文介绍了一种有效的C++算法实现,用于去除整型数组中的重复元素,仅保留唯一值。该方法通过一次遍历即可完成操作,并更新数组长度。
1105

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



