6ms C++ using unordered_map(size)


  • 0
    R

    It is important to indicate the size of map.

    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> res;
            unordered_map<int, int> map(nums1.size());
            
            for(int i = 0; i < nums1.size(); i++)
                map[nums1[i]]++;
            for(int i = 0; i < nums2.size(); i++) {
                if(map[nums2[i]] > 0) {
                    res.push_back(nums2[i]);
                    map[nums2[i]] = 0;
                }
            }
            
            return res;
        }
    };
    

Log in to reply
 

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