[LC] 350. Intersection of Two Arrays II
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的更多相关文章
- 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] 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 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
- [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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [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 ...
- leetcode349 350 Intersection of Two Arrays & II
""" Intersection of Two Arrays Given two arrays, write a function to compute their in ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- java基础——深拷贝和浅拷贝的区别
浅拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝. 深拷贝:对基本数据类型进行值传递,对引用数据类型船舰一个新的对象,并复制内容,这是深拷贝.
- JAVA基础——使用配置文件
一. 前言 日常我们做项目中,我们经常会遇到这样的情况:由于开发环境和生产环境的不同,项目部署在生产环境之前,有些参数我们并不知道如何取值.例如:数据库链接设定,我们在部署生产环境之前 ...
- openstack trove redis配置项
trove在mitaka版本更新了一个功能,configuration 具体如下: trove help |grep configuration configuration-attach Attach ...
- Mac Github:第一次上传成功,解决图片不可显示,Initial commit Untracked files
在上传之前需要先给自己的电脑安装SSH 上传成功用的是github的官方提示,直接复制去做就可以了 解决README.md中图片不可显示:图片路径到底要怎么写? https://blog.csdn.n ...
- PyQt5Day03--程序基本结构之面向对象版本+控件学习
1.程序基本结构之面向对象版本 (1)开发阶段(自己写好并测试)——设置为模版qto from PyQt5.Qt import * class Window(QWidget): def __init_ ...
- 直击JDD | 京东开启技术服务元年:携手合作伙伴,共创产业未来
11月19日,主题为"突破与裂变"的2019京东全球科技探索者大会(JDDiscovery)在京盛大开幕.京东集团副总裁黎科峰在JDD主论坛做了题为"技术驱动.开放赋能& ...
- 视频课程 | Kubernetes的兴起
视频课程 | Kubernetes的兴起 原创: 京小云 京东云开发者社区 4月3日 京东云开发者社区在3月底于北京举行了以"Cloud Native时代的应用之路与开源创新"为 ...
- 201712-1 最小差值 Java
思路: 也可以不排序,最后用abs就行 import java.util.Arrays; import java.util.Scanner; public class Main { public st ...
- 1-3.监督学习(supervised learning)
定义:监督学习指的就是我们给学习算法一个数据集,这个数据集由“正确答案”组成,然后运用学习算法,算出更多的正确答案.术语叫做回归问题 [监督学习可分为]:回归问题.分类问题.两种 例:一个学生从波特兰 ...
- (函数)P1217 [USACO1.5]回文质数 Prime Palindromes
题解: 第一次: 算法复杂度过高,导致编译超时,需要优化 #include<stdio.h>#include<math.h>int a[100000001] = { 0 };i ...