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;
}
}
 

最新文章

  1. python class metaclass instance
  2. dedecms在列表或首页取得文章首图的功能改进
  3. PAT线性结构_一元多项式求导、按给定步长反转链表、出栈序列存在性判断
  4. I/O流——字符流
  5. DOM一致性检测
  6. 什么是JDK,JRE
  7. Repeater的ItemCreated和ItemDataBind的区别
  8. Java程序的成长之路
  9. java流的性能优化1-文件复制
  10. 类图class的依赖关系
  11. clientTop,scrollTop,兼容
  12. 纯css实现翻书效果
  13. Git(介绍和安装)
  14. Java基础_0304:构造方法
  15. python实现简单工厂模式
  16. VxWorks信号量问题
  17. hibernate11--Criteria查询
  18. JavaScript大杂烩11 - 理解事件驱动
  19. C++:STL vector:sizeof(vector)
  20. fiddler4如何只抓取指定浏览器的包

热门文章

  1. ETL工具对比
  2. 2020PHP面试-网络篇
  3. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war
  4. git本地代码回滚
  5. k8常用操作
  6. Cracking Digital VLSI Verification Interview 第三章
  7. findbugs报OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE的修改实例
  8. selenium登陆qq邮箱页面
  9. 吴裕雄--天生自然MySQL学习笔记:MySQL 插入数据
  10. 我的第一次JAVA实训——校园公用房管理系统