题目描述:

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),时间复杂度太高了。

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

  1. vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
  2. {
  3. sort(nums1.begin(),nums1.end());//给nums1排序,升序
  4. sort(nums2.begin(),nums2.end());//给nums2排序,升序
  5. int s1=nums1.size(),s2=nums2.size(),i=0,j=0;//i表示nums1元素的位置,j表示nums2元素的位置
  6. vector<int>res;//存储最后结果的vector
  7. while(i<s1&&j<s2)//两个vector一旦有一个遍历完了,那么就结束比较
  8. {
  9. if(nums1[i]<nums2[j])
  10. {
  11. while(nums1[i]<nums2[j]&&i<s1)//一直找,直到nums1[i]>=nums2[j]
  12. i++;
  13. if(i==s1)//如果i已经到了nums1的外面,那么结束比较
  14. break;
  15. }
  16. else if(nums1[i]>nums2[j])
  17. {
  18. while(nums1[i]>nums2[j]&&j<s2)//一直找,直到nums2[j]>=nums1[i]
  19. j++;
  20. if(j==s2)//如果j已经到了nums2的外面,那么结束比较
  21. break;
  22. }
  23. if(nums1[i]==nums2[j])//如果刚好相等,那么插入到res中,更新i和j的值
  24. {
  25. res.push_back(nums1[i]);
  26. i++;
  27. j++;
  28. }
  29. }
  30. return res;
  31. }

上述代码实测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. Bind搭建DNS服务

    DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...

  2. [Selenium]怎样验证页面是否有无变化

    验证方法:将两次的Dom结构进行对比 String beforeStr = (String) SeleniumUtil.getInnerHTML(page.getDriver(), page.getD ...

  3. Java JarFile 解析

    Java JarFile 解析 package com.github.binarylei; import java.io.*; import java.net.URL; import java.net ...

  4. 图灵社区 书单推荐:成为Java顶尖程序员 ,看这11本书就够了

    java书单推荐 转自 http://www.ituring.com.cn/article/211418 “学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两 ...

  5. openstack网络管理命令

    1.获取网络列表 [root@cc ~(keystone_admin)]# neutron net-list +--------------------------------------+----- ...

  6. ubuntu 安装 zend studio

    hi,everyone!2014 年到了,是20你死还是爱你一世,世人不得而知.夜观天象,道德依旧在沦丧,经济依然在滑坡.行了,就整这几句.最近在折腾linux,这篇文章,没有什么意义.只是找找写bl ...

  7. IOC和DI

    Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制(传统J ...

  8. CGLIB实现动态代理

    JDK动态代理和CGLIB字节码生成的区别? * JDK动态代理只能对实现了接口的类生成代理,而不能针对类 * CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法   因为是继承 ...

  9. Oracle EBS Model Function Technical

    ♡.Oracle EBS(ERP)Oracle 是公司名字,这个我估计大家都知道.EBS是E-Business Suite的缩写,简单的说,就是Oracle做的一个企业级的信息化软件或者系统,里面包含 ...

  10. Linq使用中的ToList注意事项

    在使用Linq时,如果查询逻辑太复杂,可以拆分为多个Linq查询,下一个Linq在上一个Linq查询的结果上继续操作,这样逻辑清晰,又不会出错.但在使用ToList的时候需要注意,最常见碰到的错误是: ...