题目
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 next = 0;
for(int i = 0; i < A.length; i++) {
if(A[i] != elem) {
A[next++] = A[i];
}
}
return next;
}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/

本文提供了一种在数组中移除特定值并返回新长度的高效算法实现。该方法通过一次遍历完成元素的筛选,确保了O(n)的时间复杂度。

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



