题目描述:

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. 打劫房屋 · House Robber

    [抄题]: 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动报警 ...

  2. Elk+redis的配置

    1.先到网站上下载 https://www.elastic.co/cn/downloads,需要的工具 Elasticsearch,Kibana,Logstash,Filebeat. 先把redis安 ...

  3. qt QTcpServer与QTcpSocket通讯

    分类: C/C++ TCP        TCP是一个基于流的协议.对于应用程序,数据表现为一个长长的流,而不是一个大大的平面文件.基于TCP的高层协议通常是基于行的或者基于块的.          ...

  4. Java Thread系列(十)生产者消费者模式

    Java Thread系列(十)生产者消费者模式 生产者消费者问题(producer-consumer problem),是一个多线程同步问题的经典案例.该问题描述了两个共亨固定大小缓冲区的线程-即所 ...

  5. vue项目 菜单侧边栏随着右侧内容盒子的高度实时变化

    测试的时候发现,在选择模板.选择产品第二步第三步的时候.如果超出两行的话会盖住看不见,(因为高度所有统一都被写死了,又加了overflow~emmm~)所以要改成走马灯形式.如图: 那么问题来了,我步 ...

  6. 马婕 2014MBA专硕考试 报刊选读 6(转)

    http://blog.sina.com.cn/s/blog_3e66af4601016udh.html Protecting the weakest保护最弱势群体The recession may ...

  7. Plupload 上传详细讲解,Plupload 多实例上传,Plupload多个上传按钮--推荐使用

    今天帮朋友解决  Plupload  上传的问题,查了很多资料,资料还是挺全的,但是有点零零散散的,故整理好,合并发出来. 本教程包括: Plupload  上传详细讲. Plupload  多实例上 ...

  8. 排序:快速排序Quick Sort

    原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序 ...

  9. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

  10. 咏南中间件更新日志--将数据库配置独立成DBCONFIG.EXE

    咏南中间件更新日志--将数据库配置独立成DBCONFIG.EXE.