您的位置:首页 > 其它

[leetcode]349.Intersection of Two Arrays

2016-05-21 18:18 465 查看
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

Solution:

vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
{
unordered_set<int> htable;
unordered_set<int> htmp;
vector<int> ret;

for (int i = 0; i < (int)nums1.size(); i++)
htable.insert(nums1[i]);

for (int i = 0; i < (int)nums2.size(); i++)
if (htable.find(nums2[i]) != htable.end())
htmp.insert(nums2[i]);

for (auto it = htmp.begin(); it != htmp.end(); ++it)
ret.push_back(*it);

return ret;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: