multiset and set


  • 0
    X

    #In the last question , we use set_intersection to select the result we need, because the #elements in set are always uniq, so in this question, we can use multiset which allows #duplicate elements.
    class Solution {
    public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    multiset<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res;
    set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
    return vector<int>(res.begin(), res.end());
    }
    };


Log in to reply
 

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