class Solution {
//get the condition of copy or not copy
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int cur = 2;
for (int i = cur; i < n; ++i)
{
if( !(A[i] == A[cur-1] && A[i] == A[cur-2]) )
A[cur++] = A[i];
}
return cur<n?cur:n;
}
};
second time
class Solution {
public:
int removeDuplicates(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n == 0) return n;
int prevNum = A[0];
int prevCnt = 1;
int curIdx = 1;
for(int i = 1; i < n; ++i)
{
if(A[i] == prevNum)
{
if(prevCnt == 2) continue;
else A[curIdx++] = A[i], prevCnt++;
}
else
{
A[curIdx++] = A[i];
prevNum = A[i];
prevCnt = 1;
}
}
return curIdx;
}
};