zoukankan      html  css  js  c++  java
  • 算法题丨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.

    示例

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

    算法分析

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

    代码示例(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).

    附录

  • 相关阅读:
    【转载】高内聚低耦合
    【转载】locate命令的使用
    【转载】C内存对齐
    【原创】_INTSIZEOF 内存按照int对齐
    【转载】free查看内存
    Hive查询Join
    Hive数据查询
    Hive导入数据
    Hive表的修改Alter
    Hive中排序和聚集
  • 原文地址:https://www.cnblogs.com/lizzie-xhu/p/8757648.html
Copyright © 2011-2022 走看看