LeetCode [] array to find all the numbers disappear

【problem】

Given a range of 1 ≤ a [i] ≤ n (n = size of the array) of an integer array, the array element number appears twice, others only once.
Find all numbers [1, n] does not appear between the range in the array.

You can not use the extra space and time complexity of this task is the case of O (n) do? You can not assume that the array returned in the extra space.

Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]

[Code]

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i++) 
            nums[abs(nums[i])-1] = -abs(nums[abs(nums[i])-1]);
        vector<int> res;
        for(int i = 0; i < nums.size(); i++) {
            if (nums[i] > 0)  res.push_back(i+1);
        }
        return res;
    }
};

  

Guess you like

Origin www.cnblogs.com/zhudingtop/p/12130436.html