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

Leetcode 349. Intersection of Two Arrays

时间:2016-05-19 21:17:51      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

349. Intersection of Two Arrays

                                

Total Accepted: 2944           Total Submissions: 6253           Difficulty: Easy        

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.

 题目大意:找出两个数组交叉的数

技术分享
 1 class Solution {
 2 public:
 3     vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
 4         set<int> s1, s2;
 5         for(int i = 0; i < nums1.size(); i++){
 6             s1.insert(nums1[i]);
 7         }
 8         
 9         for(int i = 0; i < nums2.size(); i++){
10             if(s1.find(nums2[i]) != s1.end())
11                s2.insert(nums2[i]);
12         }
13         vector<int> v;
14         for(set<int>::iterator it = s2.begin(); it != s2.end(); it++){
15             v.push_back(*it);
16         }
17         return v;
18     }
19 };
View Code

 

Leetcode 349. Intersection of Two Arrays

标签:

原文地址:http://www.cnblogs.com/qinduanyinghua/p/5509983.html

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