标签:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
Subscribe to see which companies asked this question
Solution:
1 vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 2 { 3 unordered_set<int> htable; 4 unordered_set<int> htmp; 5 vector<int> ret; 6 7 for (int i = 0; i < (int)nums1.size(); i++) 8 htable.insert(nums1[i]); 9 10 for (int i = 0; i < (int)nums2.size(); i++) 11 if (htable.find(nums2[i]) != htable.end()) 12 htmp.insert(nums2[i]); 13 14 for (auto it = htmp.begin(); it != htmp.end(); ++it) 15 ret.push_back(*it); 16 17 return ret; 18 }
[leetcode]349.Intersection of Two Arrays
标签:
原文地址:http://www.cnblogs.com/ym65536/p/5515283.html