LeetCode 350. Intersection of Two Arrays II(数组交集)
来源:互联网 发布:sql团体培训 编辑:程序博客网 时间:2024/05/16 08:16
原题网址:https://leetcode.com/problems/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.
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 num2'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?
方法一:哈希映射计数。
public class Solution { public int[] intersect(int[] nums1, int[] nums2) { Map<Integer, Integer> map1 = new HashMap<>(); for(int num: nums1) { Integer count = map1.get(num); if (count == null) count = 1; else count ++; map1.put(num, count); } List<Integer> list2 = new ArrayList<>(); for(int num: nums2) { Integer count = map1.get(num); if (count == null) continue; list2.add(num); count --; if (count == 0) map1.remove(num); else map1.put(num, count); } int[] result = new int[list2.size()]; for(int i=0; i<list2.size(); i++) result[i] = list2.get(i); return result; }}
方法二:先排序再查找。
public class Solution { public int[] intersect(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); List<Integer> list = new ArrayList<>(); int i=0, j=0; while (i<nums1.length && j<nums2.length) { if (nums1[i] == nums2[j]) { list.add(nums1[i]); i++; j++; } else if (nums1[i] < nums2[j]) i++; else j++; } int[] result = new int[list.size()]; for(int k=0; k<list.size(); k++) result[k] = list.get(k); return result; }}
0 0
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- [LeetCode]350. Intersection of Two Arrays II(求两个数组交集 II)
- Intersection of Two Arrays II两个数组交集(重要!)
- Leetcode 350. Intersection of Two Arrays II 数组交集2 解题报告
- leetcode 350. Intersection of Two Arrays II 两个数组的交集
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- LeetCode 349. Intersection of Two Arrays(数组交集)
- 350. Intersection of Two Arrays II (求两个数组的交集,包含重复元素)
- [LeetCode] Intersection of Two Arrays II 两个数组的交集II
- (LeetCode)Intersection of Two Arrays II --- 求交集,不去重
- leetcode解题之349 & 350. Intersection of Two Arrays Java版(求数组交集))
- [easy]【leetcode】【java】 Intersection of Two Arrays II 两个数组的交集
- leetcode-350 Intersection of Two Arrays II 求两个数组的交集 面试见到了吗?
- 349.leetcode Intersection of Two Arrays(easy)[数组 交集 set]
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- [LeetCode]349. Intersection of Two Arrays(两数组的交集)
- Leetcode :Intersection of Two Arrays 两个数组的交集
- leetcode 349. Intersection of Two Arrays 数组的交集
- LeetCode 186. Reverse Words in a String II(反转单词)
- bzoj4567: [Scoi2016]背单词
- Android面试必考基础题
- LeetCode 187. Repeated DNA Sequences(重复DNA序列)
- Stream常用操作min,reduce
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- NLP相关
- 勾股定理每日一证 2
- Stream对数字处理
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- 【设计模式】创建型模式之生成器Builder
- JavaScript概览(一)--基本语法
- [BZOJ1009][HNOI2008]GT考试(AC自动机+dp+矩阵优化)
- linux 学习笔记(三):open、creat、close 函数的使用,文件的创建、打开与关闭