(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] ...
随机推荐
- Android 点击ListView(或GridView)的一个item,使其里面textview变色,点击另一个这个恢复原来颜色
今天作一个项目,就是做视频app,如果电视剧的话有许多剧集,点击一个item,播放不同的剧集,要有点击效果,并且默认是选择第一个.花费了一段时间,自己觉得有点难 度,现在和大家分享一下,下面是效果显示 ...
- C#的选择语句练习
1.请输入一个数x,若x<1,则y=x:若1<=x<10,则y=2x-1:若x>=10,则y=3x-11,要求随意输入一个x值,求出y值. 2.输入问题[你有房子吗?],若回答 ...
- 移动web开发和移动app开发的区分
1.移动web开发 这部分跟web前端开发差别不大,使用的技术都是html+css+js.区别为手机浏览器是webkit的天下,pc端是IE的天 下.手机网页可以理解成pc网页的缩小版加一些触摸特性. ...
- Office word 2013中直接调用MathType的方法
Office word 2013中直接调用MathType的方法 | 浏览:4403 | 更新:2014-02-20 14:45 | 标签: word 使用Office word 2013的用户肯定早 ...
- C++知识点
typedef typedef struct _COMSTAT { DWORD fCtsHold : ; //机构内位域的定义即变量fCtsHold占1个bit空间 DWORD fDsrHold : ...
- jQuery 简介
jQuery 简介 jQuery 库可以通过一行简单的标记被添加到网页中. jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML ...
- IPv6实验准备
这篇是我的第一篇博客,我想先对H3C的<IPv6技术>的实验部分进行实验和总结,欢迎评论转载. 本实验用的网路设备模拟器是HCL_7.1.59,hcl的这款模拟器非常耗费内存,各种报错,因 ...
- POI2012
现在才开始写 POI 是不是太弱了? -Rendezvous 怎么说呢,我发现我的代码好长啊-长啊-长啊-长长长长长长长长长长长长长长长长长长长长长长啊- 大概就是在一个内向树上搞一个类似 lca 的 ...
- C-随笔
C语言的设计哲学之一: 程序员知道自己在干什么-没有安全带! 值的类型并不是值的内在本质, 而是取决于它被使用的方式 1.#include <stdio.h>在预处理器处理的时候把stdi ...
- js生成二维码(jquery自带)
//引入js<script type="text/javascript" src="js/jquery.js"></script> &l ...