c++ intuitive solution, time O(n), space O(n)


  • 0
    M

    Use an array to count every number. Then, return the number with two and zero occurrence.

    class Solution {
    public:
        vector<int> findErrorNums(vector<int>& nums) {
            int cnt[nums.size()] = {0};
            for(int n : nums) cnt[n-1]++;
            int a = 0, b = 0;
            for(int i = 0; i < nums.size(); i++){
                if(cnt[i] == 2) a = i+1;
                if(cnt[i] == 0) b = i+1;
            }
            return {a,b};
        }
    };
    ``

Log in to reply
 

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