1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if (n <= 2) return n; 5 int wpos = 1; 6 int dups = 0; 7 int cur, last = A[0]; 8 9 for (int i=1; i<n; i++) { 10 cur = A[i]; 11 dups = (cur == last) ? dups + 1 : 0; 12 if (dups <= 1) { 13 A[wpos++] = cur; 14 } 15 last = cur; 16 } 17 return wpos; 18 } 19 };
意识流写代码,又有段日子不写了,感觉坚持一发!