[leetcode_80]Remove Duplicates from Sorted Array II

模拟题,但是做起来好费劲啊。
一次AC:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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) {
                    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--;
                } else {
                    count++;
                }
            }
        }
        return len;
    }
};
Licensed under CC BY-NC-SA 4.0