So easy with c++!


  • -2
    Z
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        vector<int> vec;
        unordered_set<int> mySet;
        for(auto i=nums1.begin();i!=nums1.end();i++)
        {
            if(mySet.find(*i)==mySet.end())
                mySet.insert(*i);
        }
        
        for(auto i=nums2.begin();i!=nums2.end();i++)
        {
            auto k=mySet.find(*i);
            if(k!=mySet.end())
            {
                mySet.erase(k);
                vec.push_back(*i);
            }
        }
        return vec;
    }

Log in to reply
 

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