(Collection)350. Intersection of Two Arrays II
/* Given two arrays, write a function to compute their intersection. Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order. */ public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Map<Integer,Integer> map=new HashMap();
List<Integer> list=new ArrayList();
for(int num : nums1){
map.put(num,map.getOrDefault(num,0)+1);
}
int count=0;
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 i=0;i<list.size();i++){
res[i]=list.get(i);
}
return res;
}
}
(Collection)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] ...
随机推荐
- 使用Burpsuite抓取手机APP的HTTPS数据
1.所需条件 · 手机已经获取root权限 · 手机已经成功安装xposed框架 · 电脑一台 2.详细步骤 2.1 在手机上面安装xposed JustTrustMe JustTrustMe是一个去 ...
- C#基础——winform应用上传图片到SQLServer数据库
前言 之前通过winform与SQL Server的交互一直局限于文本.数字等信息,都可以通过string的方式来传输,但是比如音乐.图片等特殊格式的文件要如何与SQL Server数据库进行交互呢? ...
- Windows下VTK6.0.0安装详解(CMake使用说明)
操作系统:Windows7,用到工具:Visual studio.CMake. 1.准备工作 VTK下载: 下载最新VTK稳定版(6.0.0,截至2013年7月)http://www.vtk.org/ ...
- thinkphp基于角色的权限控制详解
一.什么是RBAC 基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注. 在RBAC中,权限与角色相关联,用户通 ...
- Pattern Recognition And Machine Learning (模式识别与机器学习) 笔记 (1)
By Yunduan Cui 这是我自己的PRML学习笔记,目前持续更新中. 第二章 Probability Distributions 概率分布 本章介绍了书中要用到的概率分布模型,是之后章节的基础 ...
- linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)
头文件: #include<dirent.h> #include<sys/types.h> opendir(): 函数原型: DIR * opendir(const char* ...
- 谈事件冒泡(Bubble)和事件捕捉(capture)
事件的发生顺序 假设在一个元素中又嵌套了另一个元素并且两者都有一个onClick事件处理函数(event handler).如果用户单击元素2,则元素1和元素2的单击事件都会被触发.但是哪一个事件先被 ...
- Angular2学习之开发环境构建
一.主要资料 http://blog.csdn.net/cz_jjq/article/details/50425206 http://www.tuicool.com/articles/mi6rmuB ...
- NPOI读取Excel
项目环境:Webform framework4.0 dll版本:NPOI2.0 dotnet2.0版本 这两天要做个excel导入的功能,想到以前用过NPOI,感觉很给力,今天写了个DEMO,写的时 ...
- Android学习十一:高德地图使用
写这篇文章主要有三个目的: 1.使用高德地图api定位 2.获取天气数据 3.编程练手 文件结构 清单文件信息说明: <?xml version="1.0" encoding ...