[LeetCode]Remove Element

来源:互联网 发布:淘宝类目搜索什么意思 编辑:程序博客网 时间:2024/06/10 03:06

题目:给定一个数字集合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;    }}

1 0