/**********************************************************************************************************
Given an array and a value, remove all instances of that value in place and return the new length.
e order of elements can be changed. It doesn’t matter what you leave beyond the new length.
**********************************************************************************************************/
class Solution{
int removeElement(int A[],int n,int elem){
int index = 0;
for(int i = 0 ;i < n;i++){
if(A[i] != elem){
A[index++] = A[i];
}
}
return index;
}
}

参考资料:
LeetCode题解
本文介绍了一种在原地移除数组中特定值并返回新长度的算法,该算法允许元素顺序变化,对超出新长度的部分不做保留。通过具体代码实现展示了如何高效地完成这一任务。
2004

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



