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的形式即可:

解法一:

  1. class Solution {
  2. public:
  3. vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  4. set<int> s(nums1.begin(), nums1.end()), res;
  5. for (auto a : nums2) {
  6. if (s.count(a)) res.insert(a);
  7. }
  8. return vector<int>(res.begin(), res.end());
  9. }
  10. };

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

解法二:

  1. class Solution {
  2. public:
  3. vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  4. vector<int> res;
  5. int i = , j = ;
  6. sort(nums1.begin(), nums1.end());
  7. sort(nums2.begin(), nums2.end());
  8. while (i < nums1.size() && j < nums2.size()) {
  9. if (nums1[i] < nums2[j]) ++i;
  10. else if (nums1[i] > nums2[j]) ++j;
  11. else {
  12. if (res.empty() || res.back() != nums1[i]) {
  13. res.push_back(nums1[i]);
  14. }
  15. ++i; ++j;
  16. }
  17. }
  18. return res;
  19. }
  20. };

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

解法三:

  1. class Solution {
  2. public:
  3. vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  4. set<int> res;
  5. sort(nums2.begin(), nums2.end());
  6. for (auto a : nums1) {
  7. if (binarySearch(nums2, a)) {
  8. res.insert(a);
  9. }
  10. }
  11. return vector<int>(res.begin(), res.end());
  12. }
  13. bool binarySearch(vector<int> &nums, int target) {
  14. int left = , right = nums.size();
  15. while (left < right) {
  16. int mid = left + (right - left) / ;
  17. if (nums[mid] == target) return true;
  18. else if (nums[mid] < target) left = mid + ;
  19. else right = mid;
  20. }
  21. return false;
  22. }
  23. };

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

解法四:

  1. class Solution {
  2. public:
  3. vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  4. set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res;
  5. set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
  6. return vector<int>(res.begin(), res.end());
  7. }
  8. };

类似题目:

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 题目讲解汇总(持续更新中...)

[LeetCode] Intersection of Two Arrays 两个数组相交的更多相关文章

  1. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

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

  2. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

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

    给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示:    每个在结果中的元素必定是唯一的.    我们 ...

  4. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

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

  5. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

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

  6. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

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

  7. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  8. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  9. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

随机推荐

  1. MVP社区巡讲-云端基础架构:12月5日北京站 12月12日上海站

    紧跟当今的技术发展趋势还远远不够,我们要引领变革!加入本地技术专家社区,获取真实案例.实况培训演示以及探讨新一代解决方案.在此活动中,您将: 了解如何运用开源(OSS)技术.Microsoft 技术及 ...

  2. How to implement equals() and hashCode() methods in Java[reproduced]

    Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...

  3. php内核分析(四)-do_cli

    这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux # main 把剩下的代码增加了下注释全部贴出来了(这个是简化后的main函数,去掉了一些无关紧要的代码段): int m ...

  4. HTML5 网络拓扑图整合 OpenLayers 实现 GIS 地图应用

    在前面<百度地图.ECharts整合HT for Web网络拓扑图应用>我们有介绍百度地图和 HT for Web 的整合,我们今天来谈谈 OpenLayers 和 HT for Web  ...

  5. 『.NET Core CLI工具文档』(七)dotnet-new

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-new 翻译:dotnet-new 名称 dotnet-new -- 创建一个新的 .NET Core 项 ...

  6. SQL SERVER 竖表变成横表

    现有数据如下: Sql: select a.MODELID, max( case a.PNAME when'计划开始' then a.PVALUE end) as RStart, max( case ...

  7. Delphi_03_Delphi_Object_Pascal_基本语法_01

    这次是一个基本语法的第一部分,包括变量.变量初始化.常量.运算符.字符串等内容. { 本程序演示 Delphi Pascal 的基本语法 1.变量及变量的初始化 2.常量 3.运算符 3. 4. } ...

  8. Spring MVC中的ModelMap作用及用法

    ModelMap的作用: ModelMap对象主要用于传递控制方法传递数据到结果页面.类似于request的setAttribute方法的作用. 所以我们要想在jsp页面获取数据,只要将数据放到Mod ...

  9. [moka同学笔记]PHPexcel之excel导出和导入

    原案例来自http://www.sucaihuo.com/有修改 1.目录结构(文件不用解释,应该都可以看得懂,直接看代码)

  10. 脚本化CSS类-HTML5 classList属性

    HTML元素可以有多个CSS类名,class属性保存了一个用空格隔开的类名列表.标识符class在JavaScript中是保留字,所以在JavaScript中可以用className. //如下代码设 ...