###### 349. Intersection of Two Arrays

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.

note：

1、结果集中的每一个元素必须唯一；

2、结果集可以是任何顺序

class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {

sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
int c1 = 0;
int c2 = 0;
vector<int> result;

while(c1<nums1.size() && c2<nums2.size()){
if(nums1[c1]==nums2[c2]){
if(!result.size() || result.back()!=nums1[c1]){
result.push_back(nums1[c1]);
}
c1++;
c2++;
}else if(nums1[c1]>nums2[c2]){
c2++;
}else{
c1++;
}
}

return result;
}
};


class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {

unordered_set<int> interSet(nums1.begin(), nums1.end());
vector<int> result;

for(auto num : nums2){
if(interSet.count(num)){
result.push_back(num);
interSet.erase(num);
}
}

return result;
}
};


#### leetcode_349 Intersection of Two Arrays

2016-05-28 18:13:55

#### Leetcode 349. Intersection of Two Arrays 解题报告 Python Java

2016-05-18 23:30:59

#### [leetcode] 349. Intersection of Two Arrays 解题报告

2016-05-18 14:44:57

#### LeetCode 349. Intersection of Two Arrays 题解（C++）

2016-10-11 15:44:20

#### leetcode 记录 349. Intersection of Two Arrays

2016-05-18 21:09:57

#### 349. Intersection of Two Arrays I &&II (Java)

2016-05-27 12:56:58

#### [leetcode] 349. Intersection of Two Arrays

2016-05-19 16:01:01

#### 350. Intersection of Two Arrays II [easy] (Python)

2016-05-25 10:15:52

#### LeetCode:Intersection of Two Arrays

2016-05-22 10:38:33

#### leetcode_350 Intersection of Two Arrays II

2016-05-28 18:17:30

## 不良信息举报

349. Intersection of Two Arrays