leetcode 350. Intersection of Two Arrays II 两个数组的交集
来源:互联网 发布:hbuilder手机版软件 编辑:程序博客网 时间:2024/06/05 09:24
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.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
和上一道题leetcode 349. Intersection of Two Arrays 数组的交集 做法一样,直接使用HashMap来做就可以了,注意这里存在重复的元素。
代码如下:
import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;/* * 使用HashMap要不使用Set高明多了 * */public class Solution { public int[] intersectByHashMap(int[] nums1, int[] nums2) { if(nums1==null || nums1.length<=0 || nums2==null || nums2.length<=0) return new int[0]; List<Integer> res=new ArrayList<Integer>(); Map<Integer, Integer> map1=new HashMap<>(); for(int i=0;i<nums1.length;i++) map1.put(nums1[i], map1.getOrDefault(nums1[i], 0)+1); for(int i=0;i<nums2.length;i++) { if(map1.containsKey(nums2[i]) && map1.get(nums2[i])>0) { res.add(nums2[i]); map1.put(nums2[i], map1.get(nums2[i])-1); } } int[] res1=new int[res.size()]; for(int i=0;i<res1.length;i++) res1[i]=res.get(i); return res1; } /* * 使用排序去做 * */ public int[] intersect(int[] nums1, int[] nums2) { if(nums1==null || nums1.length<=0 || nums2==null || nums2.length<=0) return new int[0]; List<Integer> res=new ArrayList<Integer>(); Arrays.sort(nums1); Arrays.sort(nums2); int i=0,j=0; while(i<nums1.length && j<nums2.length) { if(nums1[i]==nums2[j]) { res.add(nums1[i]); i++; j++; }else if(nums1[i] > nums2[j]) j++; else i++; } int[] res1=new int[res.size()]; for(int k=0;k<res1.length;k++) res1[k]=res.get(k); return res1; }}
阅读全文
0 0
- leetcode 350. Intersection of Two Arrays II 两个数组的交集
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- [LeetCode] Intersection of Two Arrays II 两个数组的交集II
- [LeetCode]350. Intersection of Two Arrays II(求两个数组交集 II)
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- Leetcode :Intersection of Two Arrays 两个数组的交集
- [easy]【leetcode】【java】 Intersection of Two Arrays II 两个数组的交集
- leetcode-350 Intersection of Two Arrays II 求两个数组的交集 面试见到了吗?
- leetcode—350. Intersection of Two Arrays II 求两个list的交集
- 350. Intersection of Two Arrays II (求两个数组的交集,包含重复元素)
- Intersection of Two Arrays II两个数组交集(重要!)
- 349. Intersection of Two Arrays | 找两个数组的交集
- [LeetCode]349. Intersection of Two Arrays(两数组的交集)
- leetcode 349. Intersection of Two Arrays 数组的交集
- Leetcode 350. Intersection of Two Arrays II 数组交集2 解题报告
- 350. Intersection of Two Arrays II | 查找两个数组重复
- (LeetCode)Intersection of Two Arrays II --- 求交集,不去重
- kk《必然》后感之'形成'
- java.lang.IllegalArgumentException: Update/delete queries cannot be typed
- HOJ 2739 The Chinese Postman Problem 带权有向图上的中国邮路问题
- linux/unix多线程/多进程编程总结(一)
- php和android的json解析
- leetcode 350. Intersection of Two Arrays II 两个数组的交集
- top和ps取长补短续篇2之相同选项的不同作用
- Java设计模式-装饰者设计模式
- POJ 3680 Intervals 区间覆K次 网络流问题
- 【模板】埃氏筛
- hdu
- 剑指offer---二叉搜索树与双向链表
- 初识GCC、MAKE、GDB
- 使用pyspark进行机器学习(分类问题)