(Collection)350. Intersection of Two Arrays II
2016-06-25 15:07
260 查看
/* 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; } }
相关文章推荐
- ROS实时采集Android的图像和IMU数据
- 打字练习
- Protocol Buffer的C++入门教程
- Protocol Buffer的C++入门教程
- 2016年北京中考数学压轴题
- 【leetcode】20. Valid Parentheses
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
- Android:Eclipse 安装Genymotion插件的时候出现 There are no categorized items
- 泛型运用
- Android:支持多选的本地相册
- yii2框架安装运行init.bat报错php.exe不是内部或外部命令
- [leetcode] 351. Android Unlock Patterns 解题报告
- Servlet过滤器实现统计网站访问量
- Bugtags 与其它产品的区别
- 微信(2)
- 宝体
- 宝体
- Spring、SpringMVC、mybaits框架搭建
- jquery选择器
- eclipse中tomcat内存溢出设置