排序, 然后 找到与目标相同的数列 都干掉
1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (n == 0) return 0; 7 sort(A, A + n); 8 int open = -1, close = -1; 9 for (int i = 0; i < n; ++i) {10 if (open == -1 && A[i] == elem) {11 open = i;12 }13 if (open >= 0 && (i == n - 1 || A[i + 1] != elem)) {14 close = i;15 break;16 }17 }18 if (open == -1 && close == -1) {19 return n;20 }21 int d = close - open;22 for (int i = open; i + d + 1 < n; ++i) {23 A[i] = A[i + d + 1];24 }25 return n - d - 1;26 }27 };