码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]349.Intersection of Two Arrays

时间:2016-05-21 18:55:39      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

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:

 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!