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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null) {
return null;
}
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
for (int num: nums1) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (int num: nums2) {
if (map.containsKey(num) && map.get(num) > 0) {
list.add(num);
map.put(num, map.get(num) - 1);
}
}
int[] res = new int[list.size()];
for (int k = 0; k < res.length; k++) {
res[k] = list.get(k);
}
return res;
}
}
 

[LC] 350. Intersection of Two Arrays II的更多相关文章

  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. ...

  2. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  3. 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 ...

  4. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  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

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  7. [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 ...

  8. leetcode349 350 Intersection of Two Arrays & II

    """ Intersection of Two Arrays Given two arrays, write a function to compute their in ...

  9. LeetCode 350. Intersection of Two Arrays II

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

随机推荐

  1. PTA 天梯赛 L1

    L1-002 打印沙漏 细节:就是在  (i>j&&i+j<r+1) 这个区间里才有空格,然后就是 for 循环   for(r=1; ;r+=2)  条件不满足之后还会再 ...

  2. c++ 正则表达式查找

    C++ 正则表达式的使用 需求: 字符串含有除[0-9a-z]之外的字符,均返回失败! #include<regex> smatch result; string reg_str = &q ...

  3. VUE.js入门学习(2)-基础精讲

    1.VUE 实例 - 一个项目是有很多的vue实例拼装的.每一个组建就是vue的实例. var vm = new Vue() 2.VUE 实例生命周期钩子 生命周期函数:VUE实例在某一个时间点会自动 ...

  4. python全局变量、回调函数

    1.python全局变量相关概念及使用 来自菜鸟教程上的例子: http://www.runoob.com/python3/python3-function.html 一.python入参需要注意地方 ...

  5. vmbox 导入虚拟电脑之后无法上网

    先执行 ip addr 查看有没有分配ip 用root执行dhclient -v命令去通过DHCP协议获取一个ip,在下图的最后一行可以看到ip已经分配成功dhclient命令可以用来释放你的电脑的I ...

  6. jquery_ajax 异步提交

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. hdu2457(最少替换多少个字符使主串不包含模式串)ac自动机+dp

    题:http://acm.hdu.edu.cn/showproblem.php?pid=2457 题意:给定n个模式串,给定一个主串,问最替换掉多少个字符使主串不包含模式串或输出“-1”表示没有可行的 ...

  8. js判断苹果和安卓端或者wp端

    最近做了一个H5,说要提供一个底部,可以区分安卓或者ios,到相应的网址进行下载APP,如图: 代码如下:  window.onload = function () { var u = navigat ...

  9. awk下 gsub函数用法

     (2012-03-27 01:37:28) 标签: awk gsub linux 函数 it 分类: linux gsub函数则使得在所有正则表达式被匹配的时候都发生替换 gsub(regular ...

  10. Mac技巧-如何切换至 Mac 地图应用的卫星视图模式

    如何切换至Mac地图应用的卫星视图模式?很多刚接触MAC电脑的小伙伴并不是很清楚,今天MACW小编就教教大家切换至 Mac 地图应用的卫星视图模式该怎么做.原文:https://www.macw.co ...