Leetcode 349. Intersection of Two Arrays

Posted 琴影

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 349. Intersection of Two Arrays相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode-349 Intersection of Two Arrays

Leetcode 349. Intersection of Two Arrays

LeetCode(349)Intersection of Two Arrays

leetcode349. 两个数组的交集

LeetCode_349. Intersection of Two Arrays

[leetcode]349.Intersection of Two Arrays