题目:
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 num=0;
int len=A.length;
for(int i=0;i<len;i++){
if(A[i]!=elem)
A[num++]=A[i];
}
return num;
}
}---EOF---
本文介绍了一种在原数组中移除指定值并返回新长度的算法实现。该方法通过一次遍历数组来完成元素的替换,适用于需要改变数组元素顺序且不关注新长度之后内容的场景。

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



