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 that do not appear in this array.

Could you do it without extra space and in (O(n)) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

别人的妙想,实在佩服:
(O(n)) time, (O(1)) extra space.
重点: 扫数组,做下面的动作
nums[nums[i] -1] = -nums[nums[i]-1]
e.g.

 4		 3		 2		 7		8		2		 3		 1
-4		-3		-2		-7		8		2		-3		-1
vector<int> findDisappearedNumbers(vector<int>& A) {
    vector<int> re;
    int p, i;
    for (i = 0; i < A.size(); i++) {
        p = abs(A[i]) - 1;
        if (A[p] > 0) A[p] = -A[p];
    }
    for (i = 0; i < A.size(); i++)
        if (A[i] > 0) re.push_back(i + 1);
    return re;
}
原文地址:https://www.cnblogs.com/ZhongliangXiang/p/7359482.html