c++ solution O(1) space


  • 0
    Q
    class Solution {
    public:
        vector<int> findErrorNums(vector<int>& nums) {
            int t = 0,s=0;
            vector<int> res;
            for(int i=1;i<=nums.size();i++)
                t+=i;
            
            for(int i=0;i<nums.size();i++){
                s += abs(nums[i]);
                int idx = abs(nums[i]);
                if(nums[idx-1] < 0)
                    res.push_back(idx);
                nums[idx-1] *= -1;
            }
            int diff = t - s;
            res.push_back(res[0] + diff);
            return res;
        }
    };
    

Log in to reply
 

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