【LeetCode】Remove Element

时间:2014-05-05 12:58:57   收藏:0   阅读:294

题目:

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

【LeetCode】Remove Element,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!