Given an array of integers, 1≤a[i]≤ n (n = size of array), some elements appear twice and others AppE AR once.
Find all the elements, appear twice in this array.
Could do it without extra space and in O (n) runtime?
Example:
input:[4,3,2,7,8,2,3,1]output:[2,3]
This problem gives us an array, the numbers in the array may appear once or two times, let's find out all the numbers that appear two times, because before doing a similar topic find the Duplicate number, so not completely. An important condition of this type of problem is 1≤a[i]≤n (n = size of array), otherwise it is difficult to complete in O (1) space and O (n) time. First, a positive and negative substitution method, the core of which is to find nums[i] and Nums[nums[i]-1] relationship, we practice is, for each nums[i], we will its corresponding nums[nums[i]-1] to the opposite number, if it is already negative, The description has existed before and we will add it to the result res, see the code below:
Solution One:
classSolution { Public: Vector<int> Findduplicates (vector<int>&nums) {Vector<int>Res; for(inti =0; I < nums.size (); ++i) {intIDX = ABS (Nums[i])-1; if(Nums[idx] <0) Res.push_back (idx +1); NUMS[IDX]= -Nums[idx]; } returnRes; }};
The following method is to displace nums[i] to its corresponding position nums[nums[i]-1] up, such as the correct order of no duplicates should be [1, 2, 3, 4, 5, 6, 7, 8], and we are now [4,3,2,7,8,2,3,1], We need to move the numbers to the right position, such as the first 4 should be swapped with 7 positions, and so on, the final order should be [1, 2, 3, 4, 3, 2, 7, 8], we finally in the corresponding position test, if nums[i] and i+1, then we will nums[i] Deposit the result in res, see the code below:
Solution Two:
classSolution { Public: Vector<int> Findduplicates (vector<int>&nums) {Vector<int>Res; for(inti =0; I < nums.size (); ++i) {if(Nums[i]! = nums[nums[i]-1]) {swap (nums[i], nums[nums[i]-1]); --i; } } for(inti =0; I < nums.size (); ++i) {if(Nums[i]! = i +1) Res.push_back (Nums[i]); } returnRes; }};
The following method is in the nums[nums[i]-1] position to accumulate the array length n, note that nums[i]-1 may be out of bounds, so we need to the N, the last to find the number of two times only need to see the value of nums[i] is greater than 2n, and finally traversed Nums[i] array is [ 12, 19, 18, 15, 8, 2, 11, 9], we found that there are two numbers 19 and 18 greater than 2n, then you can get the correct results by i+1 2 and 3, see the code below:
Solution Three:
classSolution { Public: Vector<int> Findduplicates (vector<int>&nums) {Vector<int>Res; intn =nums.size (); for(inti =0; I < n; ++i) {nums[(Nums[i]-1)% n] + =N; } for(inti =0; I < n; ++i) {if(Nums[i] >2* N) res.push_back (i +1); } returnRes; }};
Similar topics:
Find the Duplicate number
Resources:
Https://discuss.leetcode.com/topic/64759/very-simple-c-solution
Https://discuss.leetcode.com/topic/64735/java-simple-solution/2
Https://discuss.leetcode.com/topic/64744/2-pass-o-1-space-solution
Leetcode all in one topic summary (continuous update ...)
[Leetcode] Find all duplicates with an array to find all duplicates in the array