13ms C++,using set and vector


  • 0
    B
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ret;
        set<int> haha;
        set<int> hehe;
        set<int>::iterator it;
        for(int i = 0; i<nums1.size();i++)
        {
            hehe.insert(nums1[i]);
        }
        for(int j = 0;j<nums2.size();j++)
        {
            it = hehe.find(nums2[j]);
            if(it != hehe.end())
            {
                haha.insert(nums2[j]);
            }
        }
        for(it = haha.begin();it!=haha.end();it++)
        {
            ret.push_back(*it);
        }
        return ret;

Log in to reply
 

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