C++ O(n) in-place solution


  • 0
    M
    class Solution {
    public:
        vector<int> findDisappearedNumbers(vector<int>& nums) {
            vector<int> temp(nums.size()); 
            int i;
            for(i = 0; i < nums.size(); ++i){
                temp[nums[i] - 1] = 1;
            }
            
            int counter = 0;
            for(i = 0; i < temp.size(); ++i){
                if(temp[i] == 0){
                    temp[counter] = i + 1;
                    counter++;
                }
            }
            temp.resize(counter);
            return temp;
        }
    };
    

Log in to reply
 

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