LeetCode每日一题:remove element

2017-07-04  本文已影响11人  yoshino

问题描述

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.

问题分析

这题让我们移除重复元素,说好的顺序可以无所谓,但实际通过不了AC。

代码实现

    public int removeElement(int[] A, int elem) {
        int index = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] != elem) {
                A[index] = A[i];
                index++;
            }
        }
        return index;
    }
上一篇下一篇

猜你喜欢

热点阅读