Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!n) return 0;
int count = 0;
for(int i = 1;i < n ;i ++)
{
if(A[count] == A[i])
continue;
else
{
count ++;
A[count] = A[i];
}
}
return count +1;
}
};
本文提供了一个C++解决方案,用于在不使用额外空间的情况下从已排序数组中移除重复元素,并返回处理后的数组长度。示例代码展示了如何通过比较相邻元素并跳过重复项来实现这一目标。

1068

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



