【leetcode75】Intersection of Two Arrays(数组的交集)
2016-07-30 21:54
330 查看
题目描述:
给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次
例如:
nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].原文描述:
Given two arrays, write a function to compute their intersection.Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:
Each element in the result must be unique.The result can be in any order.
思路一:
1.使用HashMap(Integer,Boolean)数据结构,首先是便利Array1,放入map12.遍历Array2,判断map1是否包含,存入map2
3.取出map2的数据,存入数组输出
代码:
public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { HashMap<Integer, Boolean> map1 = new HashMap<Integer, Boolean>(); HashMap<Integer, Boolean> intersectMap = new HashMap<Integer, Boolean>(); for (int i = 0; i < nums1.length; i++) { if (!map1.containsKey(nums1[i])) { map1.put(nums1[i], true); } } for (int j = 0; j < nums2.length; j++) { if (map1.containsKey(nums2[j]) && !intersectMap.containsKey(nums2[j])) { intersectMap.put(nums2[j], true); } } int[] result = new int[intersectMap.size()]; int i = 0; for (Integer e : intersectMap.keySet()) { result[i] = e; i++; } return result; } }
思路二:
先把两个数组排序索引i,j分别代表Array1和Array2,相等都加1,谁小谁对应的索引加1
-
代码:
public class Solution { /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); int i = 0, j = 0; int[] temp = new int[nums1.length]; int index = 0; while (i < nums1.length && j < nums2.length) { if (nums1[i] == nums2[j]) { if (index == 0 || temp[index - 1] != nums1[i]) { temp[index++] = nums1[i]; } i++; j++; } else if (nums1[i] < nums2[j]) { i++; } else { j++; } } int[] result = new int[index]; for (int k = 0; k < index; k++) { result[k] = temp[k]; } return result; } }
更多leetcode题目,请看我的leetcode专栏。链接如下:
leetcode专栏我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
相关文章推荐
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- [easy]【leetcode】【java】 Intersection of Two Arrays II 两个数组的交集
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- Leetcode :Intersection of Two Arrays 两个数组的交集
- [LeetCode] Intersection of Two Arrays II 两个数组的交集II
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
- Leetcode - Hash Table - 349+350 Intersection of Two Arrays(快速返回两数组重叠部分)
- (LeetCode)Intersection of Two Arrays II --- 求交集,不去重
- Intersection of Two Arrays II两个数组交集(重要!)
- [LeetCode] Intersection of Two Arrays 两个数组相交
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- 350_LeetCode_350 Intersection of Two Arrays II 题解
- 【leetcode76】Intersection of Two Arrays II
- Leetcode-349 Intersection of Two Arrays
- Leetcode #350 Intersection of Two Arrays II
- 【LeetCode-面试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
- leetCode---Intersection of Two Arrays