问题
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.
代码
class Solution {
public:
int removeElement(int A[], int n, int elem) {
if (n == 0) {
return n;
}
int slot = 0;
for (int i = 0 ; i < n ; i++) {
if (elem == A[i]) {
continue;
}
else{
A[slot] = A[i];
slot ++;
}
}
return slot;
}
};
分析
方法和 rm dup elem 方法类似。
总结
n/a