448. 找到所有数组中消失的数字

题目:给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。找到所有在 [1, n] 范围之间没有出现在数组中的数字。

示例:

输入:
[4,3,2,7,8,2,3,1]

输出:
[5,6]

题解:

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        int n = nums.size();
        vector<int> res;
        for (int num:nums){
            int tmp = (num-1)%n;
            nums[tmp]+=n;
        }
        for(int i=0;i<n;++i){
            if(nums[i]<=n)
                res.push_back(i+1);
        }
        return res;
    }
};
posted @ 2021-04-22 11:33  USTC丶ZCC  阅读(34)  评论(0编辑  收藏  举报