【leetcode75】Intersection of Two Arrays(数组的交集)
来源:互联网 发布:大数据与人工智能关系 编辑:程序博客网 时间:2024/06/05 09:36
题目描述:
给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次
例如:
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,放入map1
2.遍历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面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
0 0
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- 349. Intersection of Two Arrays | 找两个数组的交集
- [LeetCode] Intersection of Two Arrays 两个数组的交集
- [LeetCode]349. Intersection of Two Arrays(两数组的交集)
- Leetcode :Intersection of Two Arrays 两个数组的交集
- leetcode 349. Intersection of Two Arrays 数组的交集
- LeetCode 349. Intersection of Two Arrays(数组交集)
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- Intersection of Two Arrays II两个数组交集(重要!)
- 349. Intersection of Two Arrays (求两个数组的交集,不包含重复元素)
- 350. Intersection of Two Arrays II (求两个数组的交集,包含重复元素)
- 349. Intersection of Two Arrays 两个数组交集
- 349.leetcode Intersection of Two Arrays(easy)[数组 交集 set]
- [easy]【leetcode】【java】 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 两个数组的交集
- Leetcode:350. Intersection of Two Arrays II 求两个数组的交集
- DUMP文件分析2:一个最简单的DUMP分析示例
- 【Mybatis】深入浅出MyBatis(2)-Configuration
- android之ActivityManager
- 架构漫谈(一):什么是架构?
- Arm linxu启动过程分析(四)
- 【leetcode75】Intersection of Two Arrays(数组的交集)
- 线程同步:条件变量的使用细节分析
- C语言反汇编入门实例
- 【Linux】线程和死锁
- POJ 2109 Power of Cryptography(坑到没朋友)
- 【Mybatis】深入浅出MyBatis(3)-MapperBuilder
- hdoj5778abs
- 50. Pow(x, n)
- 卷积神经网络(CNN)新手指南