Remove Element
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int cnt = 0;
for(int i=0;i<n;i++)
{
if(A[i] != elem)
{
A[cnt++] = A[i];
}
}
return cnt;
}
};
使用一个cnt记录不相同元素的,当遇到相同元素就跳过cnt不记录,最后就会将所有不同于elem的元素记录下来了

浙公网安备 33010602011771号