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]
.
1、这样简单的题居然也浪费了不少时间……
2、不能判断A==nullptr,可能是因为哪怕是长度为空的数组,其指针也不是空的。
class Solution {
public:
int removeDuplicates(int A[], int n) {
int i,j;
if(n==0)
return 0;
j=0;
for(i=0;i<n;i++)
{
if(i==0)
A[j]=A[0];
else
{
if(A[i]!=A[j])
{
A[++j]=A[i];
}
}
}
A[++j]='\0';
return j;
}
};