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

448. Find All Numbers Disappeared in an Array

上一篇:微信小程序,购物车模块代码解读


下一篇:【微信开发】-- 发送模板消息