My solution using multiset


  • 0
    B


    class Solution {
    public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    multiset<int> ms(nums1.begin(), nums1.end());
    vector<int> result;
    for (vector<int>::iterator itr=nums2.begin();
    itr != nums2.end();
    ++itr) {
    multiset<int>::iterator mitr = ms.find(*itr);
    if (mitr != ms.end()) {
    result.push_back(*itr);
    ms.erase(mitr);
    }
    }
    return result;
    }
    };


Log in to reply
 

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