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.
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n < 1) return 0;
int index = 0;
int pre = 0;
while(pre < n){
if(A[pre] == elem) pre++;
else{
if(pre == index){pre++; index++;}
else A[index++] = A[pre++];
}
}
return index;
}

306

被折叠的 条评论
为什么被折叠?



