思路很简单,从前往后类似链表的形式做就可以了。
class Solution {
public:
int removeDuplicates(int A[], int n) {
int curr = -1;
for (int i = 0; i < n - 1; ++i) {
if (A[i] != A[i + 1]) {
curr++;
A[curr] = A[i];
}
}
if (A[n - 1] != A[curr]) {
curr++;
A[curr] = A[n - 1];
}
return curr + 1;
}
};
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/