标签:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
Follow up:
Subscribe to see which companies asked this question
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { map<int, int> map; vector<int> ret; for (auto &e : nums1) ++map[e]; for (auto &e : nums2) { if (map.find(e) != map.end() && map[e] !=0) { ret.push_back(e); --map[e]; } } return ret; } };
LeetCode 350. Intersection of Two Arrays II
标签:
原文地址:http://www.cnblogs.com/csudanli/p/5890938.html