美文网首页
LeetCode每日一题:remove element

LeetCode每日一题:remove element

作者: yoshino | 来源:发表于2017-07-04 10:18 被阅读11次

    问题描述

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

    相关文章

      网友评论

          本文标题:LeetCode每日一题:remove element

          本文链接:https://www.haomeiwen.com/subject/mcmthxtx.html