Hashmap C++ solution, easy to understand


  • 1
    B
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
        {
            map<int, int> table;
            
            for(int i = 0; i < nums1.size(); ++i) table[nums1[i]]++;
            
            vector<int> ret;
            for(int i = 0; i < nums2.size(); ++i)
            {
                if(table[nums2[i]])
                {
                    ret.push_back(nums2[i]);
                    table[nums2[i]]--;
                }
            }
            return ret;
        }

Log in to reply
 

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