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) {
if(n==0)
return 0;
else
{
int temp=A[0],length=1;
for(int i=1;i<=n-1;i++)
{
if(*(A+i)==temp)
continue;
else
{
temp=*(A+i);
*(A+length)=*(A+i);
length++;
}
}
return length;
}
}
};
本文介绍了一种在原地删除已排序数组中重复元素的方法,确保每个元素只出现一次,并返回新长度。该方法不使用额外空间,通过一次遍历实现,符合常数内存限制。
3588

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



