448. Find All Numbers Disappeared in an Array 找出数组中消失的数

来源:互联网 发布:mac升级显卡 编辑:程序博客网 时间:2024/06/06 09:07

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

给出一个整数数组,满足1 ≤ a[i] ≤ n (n是数组之长),有些元素出现两次,有些元素出现一次。

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

找到整数区间[1, n]中所有未在上面给出的数组中出现过的数字。

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

你能够不使用额外的空间,并且算法复杂度满足 O(n)吗? 你可以假定返回的列表不算额外空间。

Example:

Input:[4,3,2,7,8,2,3,1]Output:[5,6]


/** * @param {number[]} nums * @return {number[]} */var findDisappearedNumbers = function(nums) {    var a =[];    for( var k = 0; k<= nums.length; k++) {        a[k] = k;    }    a[0] = -1;for( var i = 0; i < nums.length; i++)  {        if(nums[i])  {            a[nums[i]] = -1;        }    }       for( var j = nums.length; j >= 0; j--) {        if (a[j] == -1) {          a.splice(j, 1);        }    }    return a;};



0 0