Remove element

来源:互联网 发布:网络通信公司简介 编辑:程序博客网 时间:2024/05/30 23:03

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 int removeElement(int[] A, int elem) {        int i = -1, j = 0;        while (i < A.length && j < A.length) {            if (A[j] == elem) {                j++;            } else {                A[++i] = A[j++];            }        }        return i+1;    }


0 0
原创粉丝点击