title : An integer array satisfies 1<=a[i]<=n (n is the length of the array), some elements appear once, some elements appear two times, and numbers that do not appear in the "1,n" interval are found in the array a[i]. For example, enter "4,3,2,7,8,2,3,1" and Output "5,6". Time complexity requirement is O (n), Space complexity requirement O (1)
Ideas : see many netizens say with set data structure to do, this violates the space complexity requirements, so sort or to do local. Iterate the array, put this number where it should be: for example, a[0]=4, then we put 4 to a[3], and then put the a[3] place 7 to a[6] place, place a[6] Place 3 a[2] place, put a[2] place 2 at a[1] Place, Dizzy Bar
Do this:
Step1: 7, 3,2,4, 8,2,3,1
Step2:3, 3,2,4,8,2,7, 1
Step3:2, 3,3, 4,8,2,7,1
Step4:3,2, 3,4,8,2,7,1
Step4. 1: Now think of exchange 3 and 3, but found heavy, then 3 or stay in the position,
STEP5:
Answer :
Https://github.com/honpey/codebox/blob/master/leetcode/array/p448.cpp
related :
For array 1<=a[i]<=n,
Algorithm (2) Find all Numbers disappeared in an Array