题目描述:

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

    • What if the given array is already sorted? How would you optimize your algorithm?
    • What if nums1's size is small compared to nums2's size? Which algorithm is better?
    • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

要完成的函数:

vector<int> intersect(vector<int>& nums1, vector<int>& nums2)

说明:

1、这道题给定两个vector,要求返回两个vector的交集,比如nums1=[1,2,2,1],nums2=[2,2],返回的交集是[2,2],其中有多少个相同的元素就返回多少个。返回的交集不讲究顺序。

2、这道题看完题意,熟悉leetcode的同学应该会马上想到先排序,排序之后的两个vector来比较,时间复杂度会下降很多。

如果不排序,那就是双重循环的做法,O(n^2),时间复杂度太高了。

先排序再比较的做法,代码如下:(附详解)

    vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
{
sort(nums1.begin(),nums1.end());//给nums1排序,升序
sort(nums2.begin(),nums2.end());//给nums2排序,升序
int s1=nums1.size(),s2=nums2.size(),i=0,j=0;//i表示nums1元素的位置,j表示nums2元素的位置
vector<int>res;//存储最后结果的vector
while(i<s1&&j<s2)//两个vector一旦有一个遍历完了,那么就结束比较
{
if(nums1[i]<nums2[j])
{
while(nums1[i]<nums2[j]&&i<s1)//一直找,直到nums1[i]>=nums2[j]
i++;
if(i==s1)//如果i已经到了nums1的外面,那么结束比较
break;
}
else if(nums1[i]>nums2[j])
{
while(nums1[i]>nums2[j]&&j<s2)//一直找,直到nums2[j]>=nums1[i]
j++;
if(j==s2)//如果j已经到了nums2的外面,那么结束比较
break;
}
if(nums1[i]==nums2[j])//如果刚好相等,那么插入到res中,更新i和j的值
{
res.push_back(nums1[i]);
i++;
j++;
}
}
return res;
}

上述代码实测7ms,beats 98.05% of cpp submissions。

leetcode-350-Intersection of Two Arrays II(求两个数组的交集)的更多相关文章

  1. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

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

  2. [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 ...

  3. [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  5. [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 ...

  6. LeetCode 350. Intersection of Two Arrays II

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

  7. Python [Leetcode 350]Intersection of Two Arrays II

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

  8. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  9. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  10. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

随机推荐

  1. 最长无重复字符的子串 · Longest Substring Without Repeating Characters

    [抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...

  2. java 实现模拟浏览器 访问网站

    一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等.所访问的这些页面 有的仅仅是一些普通的页面,有的需要用户登录后方可使用,或者需 ...

  3. windows下使用redis

    一.下载windows版本的Redis 官网只提供linux版本的下载 官网下载地址:http://redis.io/download Redis 没有官方的Windows版本,但是微软开源技术团队( ...

  4. sql查询磁盘空间并发预警邮件

    检测磁盘空间,如果低于设置的预警值则发出一封预警邮件,这样的事情可以用SQL server的作业可以做,关键SQL语句如下例子所示: DECLARE @TableText NVARCHAR(MAX)= ...

  5. Eclipse配置PyDev插件(配置Python环境) 及javascript相关配置

    Eclipse开发Javascript环境配置(含EXTJs配置) 来自:sayo http://www.cnblogs.com/sayo/archive////.html Eclipse开发JQue ...

  6. BASE64Encoder及BASE64Decoder编译器找不到问题

    编译器自带这两个类,但是会报错找不到,需要手动让编译器识别这个类 第一步.右键项目,然后选择properties 第二步,打开如图位置 第三部,选择如图位置,双击 第四部,add添加 更改值 改为如图 ...

  7. sublime主题选择

    Sublime Text是一个强大的令人难以置信的编辑器. 它不仅具有大量的功能,而且还有很多主题,让他看起来很漂亮. 我们以前整理过 2014年的最好的主题 ; 让我们看看最新的Sublime Te ...

  8. HRBUST1212 乘积最大 2017-03-06 15:47 59人阅读 评论(0) 收藏

    乘积最大 今年是国际数学联盟确定的"2000--世界数学年",又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一 ...

  9. ViewPage实现无限轮播画廊效果

    1. 效果图 2. 布局文件 主要使用的 android:clipChildren的意思:是否限制子View在其范围内.再父布局和viewpager中设置该属性  ,要显示三个界面 ,还要设置marg ...

  10. 8.使用Exists监控ZNode的三大Change事件

    一. zookeeper是一个分布式的协调程序(所有程序都是通过订阅它来相互感知)   1. tcp(长链接) + watcher server ->client client ->ser ...