c++ O(1) space O(n) time, using hashing


  • -1
    K

    since no one post this, here is the solution. advantage is that it doesn't change the original array

    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums) {
            int n = nums.size();
            vector<int> res(n + 1);
            for (int num : nums) {
                res[num % (n + 1)] = 1;
            }
            int i = 0;
            for (int j = 1; j < n + 1; ++j) {
                if (res[j] == 0) res[i++] = j;
            }
            res.resize(i);
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.