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]
    Runtime: 140 ms, faster than 34.36% of C++ online submissions for Find All Numbers Disappeared in an Array.

    1. class Solution {
    2. public:
    3. vector<int> findDisappearedNumbers(vector<int>& nums) {
    4. int len = nums.size();
    5. for(int i = 0; i < len; i++) {
    6. int idx = abs(nums[i]) - 1;
    7. nums[idx] = nums[idx] > 0 ? -nums[idx] : nums[idx];
    8. }
    9. vector<int> result;
    10. for(int i = 0; i < len; i++) {
    11. if(nums[i] > 0) {
    12. result.push_back(i + 1);
    13. }
    14. }
    15. return result;
    16. }
    17. };

    题目要求不能使用外部空间,因此可以考虑到内部的元素交换和计算,而 1 <= a[i] <= n 的重点提示可以使用将不存在的数值作为下标进行标注