[LintCode] Intersection of Two Arrays II 两个数组相交之二
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 两个数组相交之二的更多相关文章
- [LeetCode] 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 两个数组相交之二
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 两个数组相交II
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 350 Intersection of Two Arrays II 两个数组的交集 II
给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意: 输出结果中每个元素出现的次数, ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- 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] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
随机推荐
- 笔记本电脑关闭小键盘(即打字按P出现星号键)
开关方法:Fn + NumLk (联想电脑的NumLk 一般为F8,其他电脑自己在键盘找找罗)
- 注解:【有连接表的】Hibernate单向N->1关联
Person与Address关联:单向N->1,[有连接表的] Person.java package org.crazyit.app.domain; import javax.persiste ...
- How to use the Visual Studio
推荐一个提供VS配色方案的一个网站:StudioStyles,域名和网站同名:http://studiostyl.es/ 2. 整行剪切:Ctrl + X.光标不要选中任何文字,然后按这个快捷键就可以 ...
- Zookeeper 初体验之——伪分布式安装(转)
原文地址: http://blog.csdn.net/salonzhou/article/details/47401069 简介 Apache Zookeeper 是由 Apache Hadoop 的 ...
- 【MongoDB】3.详细命令集合
[注意:MongoDB自动将_id字段设置为主键] -------------------------------------------------------------------------- ...
- 判断网络是否连接 和 判断GPS是否连接
//判断网络是否连接 public static Boolean isNetworkEnabled(Context context){ int status=-1 //设置默认连接的状态为-1 Co ...
- java中 几种数据库连接池 的写法
JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.l ...
- Eclipse如何替换android应用图标
打开你的项目 我们看到项目里有 res这个文件夹里有以下文件夹. drawable-hdpi -------高分辨率 drawable-ldpi -------中分辨率 drawab ...
- 常用meta标签举例说明
本文是作者从百度百科和其他从网页中搜索到的资料,经综合整理,把常用meta标签列举并示例说明,仅供参考. 1.<meta charset="UTF-8"> --- ch ...
- MatLab有关路径的几个命令
窗外雨如注,国庆的第3天,没钱出去逛,更新我的博客吧 今天要说的命令有path.cd.userpath.savepath.pathtool 有两种改变启动目录的方法,第1种使用userpath和sav ...