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 The does not appear in this array.
Could do it without extra space and in O (n) runtime? Assume the returned list does not count as extra space.
Example:
input:[4,3,2,7,8,2,3,1]output:[5,6]
Code:
/** * @param {number[]} Nums * @return {number[]}*/varFinddisappearednumbers =function(nums) { let number= []; for(Let i = 0; i < nums.length; i++) {Let index= Math.Abs (Nums[i])-1;///Get index, change the number of the index position of the array to negative, then when traversing the array, if there are positive numbers, then the positive index+1 is the result we need .Nums[index] =-Math.Abs (Nums[index]); } for(Let i = 0; i < nums.length; i++) { if(Nums[i] > 0) {Number.push (i+ 1); } } returnNumber ;};
Leetcode 448. Find all Numbers disappeared in an Array