Given an array of integers with a range in 1≤a[i]≤n (n = array size), the elements in the array appear two times, and others appear only once.
Find all numbers in the [1, n] range that do not appear in the array.
Can you accomplish this task without using extra space and time complexity O (n)? You can assume that the returned array is not counted in the extra space.
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
See: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
C++:
Method One:
Class Solution {public: vector<int> finddisappearednumbers (vector<int>& nums) { vector< int> Res; for (int i = 0; i < nums.size (); ++i) { int idx = ABS (Nums[i])-1; NUMS[IDX] = (Nums[idx] > 0)? -NUMS[IDX]: Nums[idx]; } for (int i = 0; i < nums.size (); ++i) { if (Nums[i] > 0) { Res.push_back (i + 1); } } return res; }};
Method Two:
Class Solution {public: vector<int> finddisappearednumbers (vector<int>& nums) { vector <int> Res; for (int i = 0; i < nums.size (); ++i) { if (nums[i]! = Nums[nums[i]-1]) { swap (nums[i], nums[nums[i] -1]); -----;} } for (int i = 0; i < nums.size (); ++i) { if (nums[i]! = i + 1) { Res.push_back (i + 1); } } return res;} ;
Reference: https://www.cnblogs.com/grandyang/p/6222149.html
448 Find all Numbers disappeared in an array finds missing numbers from all arrays