intSolution::removeDuplicates(vector<int> &A){ int res = 0, l = 2, r = 2, n = A.size(); if(n < 3) return n; while(r < n) { if(A[r] != A[l-2]) A[l++] = A[r]; r++; } return l; }
Time :
Space :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intSolution::removeDuplicates(vector<int> &A){ int res = 0, l = 0, r = 0, n = A.size(); while(l < n) { if(l and A[l-1] == A[l]) l++; elseif(l + 1 < n and A[l] != A[l+1]) l++; else { r = l + 2; while(r < n and A[r] == A[l]) res += 1, r += 1; for(int i = l + 2, j = r; j < n; j++,i++) { A[i] = A[j]; } n -= (r - (l + 2)); l += 2; } } return A.size() - res; }