C++ inplace solution in 4 lines


  • 0
    P
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        vector<int> ans;
        for (int i = 0; i < nums.size(); ++i) nums[abs(nums[i]) - 1] *= -1;
        for (int i = 0; i < nums.size(); ++i) if (nums[i] > 0) ans.push_back(i + 1);
        return ans;
    }

  • 0
    E

    there might be a minor mistake in your code...
    in the first loop you should change it into:

     if(nums[abs(nums[i])-1]>0) nums[abs(nums[i])-1] *=1
    

Log in to reply
 

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