给定两个数组,写一个方法来计算它们的交集。
例如:
给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].
注意:
- 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
- 我们可以不考虑输出结果的顺序。
跟进:
- 如果给定的数组已经排好序呢?你将如何优化你的算法?
- 如果 nums1 的大小比 nums2 小很多,哪种方法更优?
- 如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存中,你该怎么办?
解法1
用
Map来建立nums1中字符和其出现个数之间的映射, 然后遍历nums2数组,如果当前字符在Map中的个数大于0,则将此字符加入结果res中,然后Map的对应值自减1。public int[] intersect(int[] nums1, int[] nums2) { List<Integer> tmp = new ArrayList<>(); Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums1.length; i++) { Integer value = map.get(nums1[i]); map.put(nums1[i], (value == null ? 0 : value) + 1); } for (int i = 0; i < nums2.length; i++) { if (map.containsKey(nums2[i]) && map.get(nums2[i]) != 0) { tmp.add(nums2[i]); map.put(nums2[i], map.get(nums2[i]) - 1); } } int[] result = new int[tmp.size()]; int i = 0; for (Integer e : tmp) result[i++] = e; return result; }解法2
给两个数组排序,然后用两个索引分别代表两个数组的起始位置,如果两个索引所代表的数字相等,则将数字存入结果中,两个索引均自增1,如果第一个索引所代表的数字大,则第二个索引自增1,反之亦然。
public int[] intersect(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); List<Integer> tmp = new ArrayList<>(); int i = 0; int j = 0; while (i < nums1.length && j < nums2.length) { if (nums2[j] > nums1[i]) { i++; } else if (nums2[j] < nums1[i]) { j++; } else { tmp.add(nums1[i]); i++; j++; } } int[] result = new int[tmp.size()]; for (int k = 0; k < result.length; k++) { result[k] = tmp.get(k); } return result; }
本文介绍两种计算两个整数数组交集的方法,一种是使用Map记录元素出现次数,另一种是对数组排序后双指针匹配,同时探讨了不同场景下的最优解。
254

被折叠的 条评论
为什么被折叠?



