题目描述:

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. qt5.7 安装

    http://blog.csdn.net/liang19890820/article/details/53931813#安装-qt57 安装运行出错:qt vstool 指定qt安装路径 http:/ ...

  2. 20155311 2016-2017-2 《Java程序设计》第8周学习总结

    20155311 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 通用API: •日志API • 日志: 日志对信息安全意义重大,审计.取证.入侵检测等都会用 ...

  3. SLAM应用的一些思考

    关心SLAM技术的人有两种.一是像我这样的研究者,为了了解其中各种方法和模块的原理.二是机器人技术的开发者,旨在将SLAM技术用到他们自己的机器人上.从数量上来说,第二类人数远多于第一类,他们的需求也 ...

  4. Atom 编辑器侧边栏忽略隐藏文件

    设置中配置需要忽略的文件后缀 package中找到treeview,勾选上这个配置就行

  5. asp.net core 1.1 + mysqlsugar + y-ui Demo

    最近研究下asp.net core 此源码架构 : .net core  mvc 简单三层 依赖注入(.net core自带) mysql + mysqlsugar +sqlsugarcore 需要修 ...

  6. Ansible 笔记 (1) - 安装和配置

    本文参考 <Ansible 自动化运维和最佳实践>,这两天刚读这本书,写写总结.主控机环境是 centos 7,被控机均是 centos 6.8 . 确保 python 版本大于 2.6 ...

  7. Hdu1874 畅通工程续 2017-04-12 18:37 48人阅读 评论(0) 收藏

    畅通工程续 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  8. 团体程序设计天梯赛L1-020 帅到没朋友 2017-03-22 17:46 72人阅读 评论(0) 收藏

    L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...

  9. oracle内部结构

    数据库管理系统将数据存储在磁盘.磁带以及其他的裸设备上,虽然这些设备的访问速度相比内存慢很多,但其非易失性和大容量的特点使他们成为数据存储的不二之选. 本文主要讨论大型数据库产品的磁盘存储内部结构,这 ...

  10. DATASNAP远程方法返回TSTREAM正解

    DATASNAP远程方法返回TSTREAM正解 DATASNAP远程方法返回TSTREAM,如果数据大小超过32K是会报错的.许多DELPHIER栽在这个上头,甚至开始怀疑TSTREAM返回数据的可行 ...