算法题丨Remove Element

描述

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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.git

示例

Given nums = [3,2,2,3], val = 3, Your function should return length = 2, 
with the first two elements of nums being 2.

算法分析

难度:低
分析:给定数组和指定一个目标值,从数组中移除全部跟目标值相等的元素,返回最终元素的长度,注意不要另外分配内存空间。
思路:题目很简单,直接遍历数组元素,判断当前元素是否跟目标值相等,若是不相等,证实当前元素应该留在数组中,有效数组长度自增1,不然为无效元素,由于只需返回有效数组长度,因此不用删除元素,跳过此循环便可。github

代码示例(C#)

public int RemoveElement(int[] nums, int val)
{
    int i = 0;
    for (int j = 0; j < nums.Length; j++)
    {
        //若是不相等,有效长度自增1
        if (nums[j] != val)
        {
            nums[i] = nums[j];
            i++;
        }
    }
    return i;
}

复杂度

  • 时间复杂度O (n).
  • 空间复杂度O (1).

附录

相关文章
相关标签/搜索