C++ multiset O(m+n).


  • 0
    M

    This came out cleaner than expected. Run time ~6ms:

        vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
            unordered_multiset<int> nums(begin(nums1), end(nums1));
    
            vector<int> res;
    
            for (auto val : nums2) {
                auto iter = nums.find(val);
                if (iter != nums.end()) {
                    nums.erase(iter);
                    res.push_back(val);
                }
            }
    
            return res;
        }
    

Log in to reply
 

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