Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
// Source : https://oj.leetcode.com/problems/remove-element/
// Author : Chao Zeng
// Date : 2014-12-21
//输出还要保证数组已经删除了elem
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int ans = n;
for (int i = 0; i < n; i++){
if (A[i] == elem){
ans--;
}
}
int k = 0;
for (int i = 0; i < n; i++){
if (A[i] != elem)
A[k++] = A[i];
}
return ans;
}
};