Remove Duplicates from Sorted Array II
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 cnt = 0;
int cur = -1,i;
for(i=0;i<n;i++)
{
if(cur==-1||A[cur]!=A[i])
{
cnt = 1;
++cur;
swap(&A[cur],&A[i]);
}else if(cnt==1)
{
cnt++;
++cur;
swap(&A[cur],&A[i]);
}
}
return cur+1;
}
浙公网安备 33010602011771号