zoukankan      html  css  js  c++  java
  • [Algorithm] 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

    Find all the elements of [1, n] inclusive that do not appear in this array.

    Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

    Example:

    Input:
    [4,3,2,7,8,2,3,1]
    
    Output:
    [5,6]
    /**
     * @param {number[]} nums
     * @return {number[]}
     */
    var findDisappearedNumbers = function(nums) {
        let a = new Set();
        for (let i = 1; i <= nums.length; i++) {
            a.add(i);
        }
    
        for (let num of nums) {
            if (a.has(num)) {
                a.delete(num)
            }
        }
       
        return [...a.values()];
    };
             

    The basic idea is that we iterate through the input array and mark elements as negative using nums[nums[i] -1] = -nums[nums[i]-1]. In this way all the numbers that we have seen will be marked as negative. In the second iteration, if a value is not marked as negative, it implies we have never seen that index before, so just add it to the return list.

    Because the elements in the Array are from 1 to n, so subtracting 1 will be 0 to n-1 which are the index of the array.
    Take input [4.3.2.7.8.2.3.1] as an example, by subtracting 1 it becomes [3.2.1.6.7.1.2.0] which is an array of index.
    For the first iteration
    when i = 0 , it marks the nums[3] as negative, the array become [4.3.2.-7.8.2.3.1].
    when i = 1, it marks the nums[2] as negative, the array become [4.3.-2.-7.8.2.3.1].
    when i = 2, it marks the nums[1] as negative, the array become [4.-3.-2.-7.8.2.3.1].
    when i = 3, it marks the nums[6] as negative, the array become [4.-3.-2.-7.8.2.-3.1].
    ...
    ...
    when i = 6, it marks the nums[0] as negative, the array become [-4.-3.-2.-7.8.2.-3.-1].

    For the second iteration
    find nums[4] = 8 and nums[5] = 2 which > 0;
    which means 4 and 5 are not in the index array[3.2.1.6.7.1.2.0].
    by adding 1, 5 and 6 are not in the input[4.3.2.7.8.2.3.1]
    return[5.6]

    public List<Integer> findDisappearedNumbers(int[] nums) {
            List<Integer> ret = new ArrayList<Integer>();
            
            for(int i = 0; i < nums.length; i++) {
                int val = Math.abs(nums[i]) - 1;
                if(nums[val] > 0) {
                    nums[val] *= -1;
                }
            }
            
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] > 0) {
                    ret.add(i+1);
                }
            }
            return ret;
        }
  • 相关阅读:
    MySQL SQL语句总结
    JavaFx通用查询、跳转创建、修改页面、关闭舞台工具类
    反射工具类
    SpringBoot将数据写入word模板,生成word文档
    vscode设置中文的方法
    Flex 布局
    处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” •使用了托管的处理程序,但是未安装或未完整安装 ASP.NET。
    第一周助教总结
    JS时间转换
    ES 安装常见错误
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12297660.html
Copyright © 2011-2022 走看看