zl程序教程

您现在的位置是:首页 >  其他

当前栏目

[LeetCode]Remove Element

LeetCode Element remove
2023-09-11 14:14:10 时间

题目:给定一个数字集合A。要求去除集合A中全部的elem,并返回新集合A的长度

算法:遍历覆盖

public class Solution {
    public int removeElement(int[] A, int elem) {
	        int length = 0;
	        for (int i=0; i<A.length; ++i) {
	        	if (elem != A[i]) {
	        		A[length++] = A[i];
	        	}
	        }
	        
	        return length;
	    }
}