模拟题,但是做起来好费劲啊。
一次AC:
class Solution { public: int removeDuplicates(int A[], int n) { if(n == 0) return n; int now; int count = 0; int len = n; for(int i = 0;i < len;i++) { if(count == 0 || now != A[i]) { count = 1; now = A[i]; continue; } if(A[i] == now) { if(count == 2) {<br /> int dis = 0; int tmp = A[i]; for(int j = i+1;j < len;j++) { if(tmp == A[j]) { dis++; } else { A[j-1-dis] = A[j]; } } len-=1+dis; i--; }<br /> else count++; } } return len; } };