AC C++ Using map and set to solve


  • 0
    J
    class Solution {
    public:
        vector<int> findErrorNums(vector<int>& nums) {
            int num = 0, mis = 0;
            if (nums.size() == 0)
                return {};
            unordered_map<int, int> m;
            unordered_set<int> s(nums.begin(), nums.end());
            for (int num : nums)
                m[num]++;
            for (auto it = m.begin(); it != m.end(); it++) 
                if (it->second == 2) {
                    num = it->first;
                    break;
                }
            for (int i = 1; i <= nums.size(); i++)
                if (s.find(i) == s.end())
                    mis = i;
            return {num, mis};
        }
    };

Log in to reply
 

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