Remove Element

    xiaoxiao2021-03-26  61

    题目:

    Given an array and a value, remove all instances of that value in place and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    Example: Given input array nums = [3,2,2,3], val = 3

    Your function should return length = 2, with the first two elements of nums being 2.

    题意:去除数组中跟规定相同的元素,并返回新数组的长度

    思想:这题比较简单,主要考虑一个标志,既能覆盖重复的的单元又能计算到下标,没有难度,C++代码如下所示

    class Solution { public: int removeElement(vector & nums, int val) { int startIndex = 0; int n = nums.size(); for(int i=0;i

    转载请注明原文地址: https://ju.6miu.com/read-350034.html

    最新回复(0)