[LintCode] Intersection of Two Arrays II 两个数组相交之二

时间:2022-09-12 22:44:41

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

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

Example

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
Challenge

What if the given array is already sorted? How would you optimize your algorithm?
    What if nums1's size is small compared to num2'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?

LeetCode上的原题,请参见我之前的博客Intersection of Two Arrays II

解法一:

class Solution {
public:
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
unordered_map<int, int> m;
for (auto a : nums1) ++m[a];
for (auto a : nums2) {
if (m[a] > ) {
res.push_back(a);
--m[a];
}
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param nums1 an integer array
* @param nums2 an integer array
* @return an integer array
*/
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
int i = , j = ;
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end());
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) ++i;
else if (nums1[i] > nums2[j]) ++j;
else {
res.push_back(nums1[i]);
++i; ++j;
}
}
return res;
}
};

[LintCode] Intersection of Two Arrays II 两个数组相交之二的更多相关文章

  1. &lbrack;LeetCode&rsqb; Intersection of Two Arrays II 两个数组相交之二

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

  2. &lbrack;LeetCode&rsqb; 350&period; Intersection of Two Arrays II 两个数组相交之二

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

  3. &lbrack;LeetCode&rsqb; 350&period; Intersection of Two Arrays II 两个数组相交II

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

  4. 350 Intersection of Two Arrays II 两个数组的交集 II

    给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意:       输出结果中每个元素出现的次数, ...

  5. LeetCode 349&period; Intersection of Two Arrays (两个数组的相交)

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

  6. &lbrack;LeetCode&rsqb; 349&period; Intersection of Two Arrays 两个数组相交

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

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

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

  8. &lbrack;LeetCode&rsqb; Intersection of Two Arrays 两个数组相交

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

  9. 26&period; leetcode 350&period; Intersection of Two Arrays II

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

随机推荐

  1. aspNet各种模块介绍

    For browsers that do not support HTML5, you can use Modernizr. Modernizr is an open-source JavaScrip ...

  2. RESTFul Web Api 服务框架(一)

    简介: 基于 REST 的 Web 服务日益成为后端企业服务集成的首选,因为它比 SOAP 更加简单.这篇文章介绍了一 个简单的可扩展框架,使用Asp.net Web Api作为 REST 服务的实现 ...

  3. 设计模式之 -- 单例模式&lpar;Singleton&rpar;

    单例模式是一种常用的软件设计模式,通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问. 使用说明 1.使用时机  在某些系统中某些对象最多只能存在一个,例如Windows中只能打开一个 ...

  4. Discuz使用tools修复数据文件后,访问URL多出&sol;source&sol;plugin&sol;tools&comma;导致文章栏目无法访问

    今天我的婚嫁亲子网数据库出了点错误,于是就用dz官方的tool工具修复了以下,然后就发生了这个错误.. 本来频道页面的地址是:http://www.ifen8.com/article/ 结果自动跳转成 ...

  5. 通过模板类简单实现Spark的JobServer

    实验前后效果对比: 之前:执行13个节点,耗时16分钟 之后:同样13个节点,耗时3分钟 具体逻辑请参照代码及注释. import java.util.concurrent.{ExecutorServ ...

  6. CentOS7上GitLab的使用

    生成SSH Keys 生成root账号的ssh key # ssh-keygen -t rsa -C "admin@example.com" 显示pub key的值 # cat ~ ...

  7. MongoDB基础之一&colon;Conetos下安装MongoDB

    1.下载自己需要的版本,我这用的是mongodb-linux-x86_64-2.4.9.tgz #cd /usr/local/src # wget http://fastdl.mongodb.org/ ...

  8. 关于t,f test

    我也是佛了 这么基础的概念其实每次都会搞混一些 首先我们针对variance求一个estimator s 然后对于任意置信区间 (sample mean +- 你所需的置信百分比的t * SE(SE就 ...

  9. 最新Java基础面试题及答案整理

    最近在备战面试的过程中,整理一下面试题.大多数题目都是自己手敲的,网上也有很多这样的总结.自己感觉总是很乱,所以花了很久把自己觉得重要的东西总结了一下. 面向对象和面向过程的区别 面向过程:    优 ...

  10. webdriver简介及浏览器的驱动

     1.webdriver概述:  webdriver(selenium2=selenium1+webdriver)是一种用于web应用程序的自动化测试工具,它提供了一套友好的API,与selenium ...