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.
public class Solution {
public int removeElement(int[] A, int elem) {
int index = 0;
for(int i = 0; i < A.length; i++){
if(A[i] != elem){
A[index++] = A[i];
}
}
return index;
}
}

本文介绍了一个删除数组中指定元素的算法,并在原地修改数组,返回删除后数组的新长度。该方法不改变元素之间的相对顺序,适用于各种应用场景。
5514

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



