leetcode-350-Intersection of Two Arrays II(求两个数组的交集)
题目描述:
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(求两个数组的交集)的更多相关文章
- LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [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 ...
- [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 ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [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 ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- Python [Leetcode 350]Intersection of Two Arrays II
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
随机推荐
- Bind搭建DNS服务
DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...
- [Selenium]怎样验证页面是否有无变化
验证方法:将两次的Dom结构进行对比 String beforeStr = (String) SeleniumUtil.getInnerHTML(page.getDriver(), page.getD ...
- Java JarFile 解析
Java JarFile 解析 package com.github.binarylei; import java.io.*; import java.net.URL; import java.net ...
- 图灵社区 书单推荐:成为Java顶尖程序员 ,看这11本书就够了
java书单推荐 转自 http://www.ituring.com.cn/article/211418 “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两 ...
- openstack网络管理命令
1.获取网络列表 [root@cc ~(keystone_admin)]# neutron net-list +--------------------------------------+----- ...
- ubuntu 安装 zend studio
hi,everyone!2014 年到了,是20你死还是爱你一世,世人不得而知.夜观天象,道德依旧在沦丧,经济依然在滑坡.行了,就整这几句.最近在折腾linux,这篇文章,没有什么意义.只是找找写bl ...
- IOC和DI
Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制(传统J ...
- CGLIB实现动态代理
JDK动态代理和CGLIB字节码生成的区别? * JDK动态代理只能对实现了接口的类生成代理,而不能针对类 * CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法 因为是继承 ...
- Oracle EBS Model Function Technical
♡.Oracle EBS(ERP)Oracle 是公司名字,这个我估计大家都知道.EBS是E-Business Suite的缩写,简单的说,就是Oracle做的一个企业级的信息化软件或者系统,里面包含 ...
- Linq使用中的ToList注意事项
在使用Linq时,如果查询逻辑太复杂,可以拆分为多个Linq查询,下一个Linq在上一个Linq查询的结果上继续操作,这样逻辑清晰,又不会出错.但在使用ToList的时候需要注意,最常见碰到的错误是: ...