349. Intersection of Two Arrays

来源:互联网 发布:java编程用记事本 编辑:程序博客网 时间:2024/05/18 18:21

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

Subscribe to see which companies asked this question.

class Solution {public:    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {        map<int,int> m;        int n1 =nums1.size();        int n2 = nums2.size();        for(int i =0;i<n1;i++)  m[nums1[i]]++;        vector<int> res;        for(int i=0;i<n2;i++){            if(m[nums2[i]]!=0){                res.push_back(nums2[i]);                m[nums2[i]]=0;                            }        }            return res;    }};


0 0