【LeetCode】Remove Element

来源:互联网 发布:3w咖啡盈利模式 知乎 编辑:程序博客网 时间:2024/06/06 05:23

题目:

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

0 0