Simple C++ O(n) solution, no hashset


  • 0
    M
        vector<int> findErrorNums(vector<int>& nums) {
            vector<int> n(nums.size(), 0), res(2, 0);
            for(int i=0;i<nums.size();i++) n[nums[i]-1]++;
            for(int i=0;i<n.size();i++) {
                if(n[i]==2) res[0]=i+1;
                else if(n[i]==0) res[1]=i+1;
            }
            return res;
        }

Log in to reply
 

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