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 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]
Idea: The array appears as a number, just where it should be (set the number in this position to the opposite number), and if the same number appears again, it will not change because it is already negative. After all, the array is re-scanned, and the unmarked position corresponds to a number that has not occurred (Note: position = number-1)
1 classSolution {2 Public:3vector<int> Finddisappearednumbers (vector<int>&nums) {4 intLen =nums.size ();5vector<int> res;//returned list6 for(inti =0; i < Len; i++) {7 intindex = ABS (Nums[i])-1;8 if(Nums[index] >0) {9Nums[index] *=-1;Ten } One } A - for(inti =0; i < Len; i++) { - if(Nums[i] >0) { theRes.push_back (i +1); - } - } - + returnRes; - } +};
448. Find all Numbers disappeared in an Array