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.

这道题让我们找两个数组交集的部分(不包含重复数字),难度不算大,我们可以用个set把nums1都放进去,然后遍历nums2的元素,如果在set中存在,说明是交集的部分,加入结果的set中,最后再把结果转为vector的形式即可:

解法一:

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {set<int> s(nums1.begin(), nums1.end()), res;for (auto a : nums2) {if (s.count(a)) res.insert(a);}return vector<int>(res.begin(), res.end());}
};

我们还可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的开头,然后比较两个数组的大小,把小的数字的指针向后移,如果两个指针指的数字相等,那么看结果res是否为空,如果为空或者是最后一个数字和当前数字不等的话,将该数字加入结果res中,参见代码如下:

解法二:

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {vector<int> res;int i = 0, j = 0;sort(nums1.begin(), nums1.end());sort(nums2.begin(), nums2.end());while (i < nums1.size() && j < nums2.size()) {if (nums1[i] < nums2[j]) ++i;else if (nums1[i] > nums2[j]) ++j;else {if (res.empty() || res.back() != nums1[i]) {res.push_back(nums1[i]);}++i; ++j;}}return res;}
};

我们还可以使用二分查找法来做,思路是将一个数组排序,然后遍历另一个数组,把遍历到的每个数字在排序号的数组中用二分查找法搜索,如果能找到则放入结果set中,这里我们用到了set的去重复的特性,最后我们将set转为vector即可:

解法三:

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {set<int> res;sort(nums2.begin(), nums2.end());for (auto a : nums1) {if (binarySearch(nums2, a)) {res.insert(a);}}return vector<int>(res.begin(), res.end());}bool binarySearch(vector<int> &nums, int target) {int left = 0, right = nums.size();while (left < right) {int mid = left + (right - left) / 2;if (nums[mid] == target) return true;else if (nums[mid] < target) left = mid + 1;else right = mid;}return false;}
};

或者我们也可以使用STL的set_intersection函数来找出共同元素,很方便:

解法四:

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res;set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));return vector<int>(res.begin(), res.end());}
};

类似题目:

Intersection of Two Arrays II

参考资料:

https://leetcode.com/discuss/103345/three-java-solutions

https://leetcode.com/discuss/103224/my-c-solution-with-sort

https://leetcode.com/discuss/103295/my-c-solutions-using-set-and-unordered_set

LeetCode All in One 题目讲解汇总(持续更新中...)

转载于:https://www.cnblogs.com/grandyang/p/5507129.html

[LeetCode] Intersection of Two Arrays 两个数组相交相关推荐

  1. 349. Intersection of Two Arrays 两个数组的交集

    给定两个数组,编写一个函数来计算它们的交集.   示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例 2: 输入:nums1 = [4,9,5], ...

  2. LeetCode算法题350:两个数组的交集II(Intersection of Two Arrays II)

    技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 两个数组的交集II Leet ...

  3. LeetCode Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...

  4. Leetcode: Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...

  5. leetcode系列-349.两个数组的交集

    leetcode系列–第349题.两个数组的交集 给定两个数组 nums1 和 nums2 ,返回 它们的交集 . 输出结果中的每个元素一定是 唯一 的.我们可以 不考虑输出结果的顺序 . 示例 1: ...

  6. leetcode系列-350.两个数组的交集II

    leetcode系列–第350题.两个数组的交集II 给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集. 返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一 ...

  7. 20190812:两个数组的交集ⅠⅡ

    两个数组的交集Ⅰ&Ⅱ 题目 大致思路 代码实现 题目 1:返回的数组中不能包含重复值 2.必须将所有数字列出来,包括重复的数字 大致思路 第一题的思路:使用set来实现,第一个set存放第一个 ...

  8. numpy np.matmul()(两个数组的矩阵乘积)

    from multiarray def matmul(a, b, out=None): # real signature unknown; restored from __doc__"&qu ...

  9. LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II

    Table of Contents 一.中文版 二.英文版 三.My answer 四.解题报告 一.中文版 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2 ...

最新文章

  1. keras系列︱图像多分类训练与利用bottleneck features进行微调(三)
  2. GraphQL是什么“渣渣“?它想干掉RESTful
  3. centos 安装低版本内核_Docker安装教程
  4. 【英语学习】【医学】Unit 02 The Brain and Its Functions
  5. 思科网络基础课件_网络自动化认证,你选对了吗?
  6. Cent OS 7配置Dnsmasq
  7. 【LOJ#10170】国王
  8. Android ListView下拉刷新点击加载更多
  9. JAVA求n个数里最小的k个_n个数 找到最小的k个数 几种解法 和java实现
  10. java中class.forName和classLoader加载类的区分
  11. 在cmd下载清华镜像
  12. JavaScript设计模式之享元模式
  13. ps 填充颜色快捷键
  14. python 语言基本知识2:数据结构
  15. 测试职业规划之知识点总结
  16. cad考试题库绘图题答案_最新CAD考试题库及答案-cad考试题库绘图题答案
  17. win7部署程序到模拟器上出错:无法连接到设备,错误:0x8007064a
  18. 什么是链接,为什么需要链接?
  19. 郁闷的出纳员(treap)
  20. WslRegisterDistribution failed with error: 0x80070050

热门文章

  1. 改进的EfficientNet-B4用于黑色素瘤检测
  2. python标准库os.path中_Python零基础入门学习19:常用标准库之os.path子库
  3. 动手学深度学习Pytorch Task01
  4. python写背单词软件_python实现屏保程序(适用于背单词)
  5. python二维向量运算_python中二维数组的Elementwise与or或运算
  6. python读取txt文件出现UnicodeError
  7. 安卓10省电还是费电_拍照成罪魁祸首 安卓十大耗电App排行公布
  8. Facebook发布神经蛋分离法,可从嘈杂环境中提取音视频
  9. ABBYY PDF Transformer+功能概述
  10. Nginx安装及配置文件解释