Posted on

Description

Submission

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        nums.insert(nums.begin(), 0);
        int n = nums.size();

        for(int i = 0; i < n;) {
            if(nums[i] != i && nums[i] != -1) {
                if(nums[nums[i]] == nums[i]) {
                    nums[i] = -1;
                    ++i;
                } else {    
                    swap(nums[i], nums[nums[i]]);
                }
                
            } else {
                ++i;
            }
        }

        vector<int> rets;
        for(int i = 0; i < n; ++i) {
            if(i != nums[i]) rets.push_back(i);
        }

        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *