首页 > 代码库 > 【LeetCode】Remove Element

【LeetCode】Remove Element

题目:

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---