Intersection of Two Arrays II
来源:互联网 发布:202z.com最新域名 编辑:程序博客网 时间:2024/05/18 09:30
利用map的键值,,键是数组的值,值存放该数组值在nums1中出现的次数
public int[] intersect(int[] nums1, int[] nums2) { //可以通过键值对来记录重复的数字有多少个HashMap<Integer, Integer> mp = new HashMap<>();List<Integer> ll = new ArrayList<>();for(int i = 0;i<nums1.length;i++){if(mp.containsKey(nums1[i]))mp.put(nums1[i], mp.get(nums1[i])+1);elsemp.put(nums1[i], 1);}for(int i = 0 ; i<nums2.length;i++){if(mp.containsKey(nums2[i])&&mp.get(nums2[i])>0){ll.add(nums2[i]);mp.put(nums2[i], mp.get(nums2[i])-1);}}int[] res = new int[ll.size()];for(int i = 0;i<ll.size();i++){res[i] = ll.get(i);}return res; }
0 0
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays&Intersection of Two Arrays II
- [LeetCode] Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- LeetCode Intersection of Two Arrays II
- d3选择集合的操作方法(三):lower与raise
- linux常用命令收集
- Shell脚本编程之While循环
- Intent传递对象信息方式Parcelable和Serializable(序列化)
- Oracle 一个汉语汉字 占用几个字节
- Intersection of Two Arrays II
- [Android Studio]aapt finished with non zero exit value 127
- poj 1928 <结构体排序>
- orcale数据库创建表空间,创建用户,权限
- 0-1背包问题
- Maven 手动添加 JAR 包到本地仓库
- HTML中阻止内容被选中的办法
- 网站品牌列表效果
- Seaside最短路,找到最近靠海地)