Remove Duplicates from Sorted Array
void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int cur = -1,i;
for(i=0;i<n;i++)
{
if(cur==-1||A[i]!=A[cur])
{
cur++;
swap(&A[i],&A[cur]);
}
}
return cur+1;
}
};
浙公网安备 33010602011771号