448. Find All Numbers Disappeared in an Array

Problem:

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]

思路

Solution (C++):

vector<int> findDisappearedNumbers(vector<int>& nums) {
    int n = nums.size();
    vector<int> res;
    set<int> s;
    for (auto n : nums) s.insert(n);
    for (int i = 1; i <= n; ++i) {
        if (s.find(i) == s.end()) res.push_back(i);
    }
    return res;
}

性能

Runtime: 308 ms  Memory Usage: 25.8 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

相关链接如下:

知乎:littledy

欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/dysjtu1995/p/12596197.html