C++ unordered_map 16ms


  • -1
    G
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            unordered_map<int,int>um;
            for(int i=0;i<nums1.size();i++){
                um[nums1[i]]=1;
            }
            for(int i=0;i<nums2.size();i++){
                if(um[nums2[i]]==1){
                    um[nums2[i]]=2;
                }
            }
            vector<int>res;
            for(auto it=um.begin();it!=um.end();it++){
                if(it->second==2){
                    res.push_back(it->first);
                }
            }
            return res;
        }
    };

Log in to reply
 

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