新手做地方门户网站,苏州建设工程招标网,做网站大概要花多少钱,网站建设管理及维护哈喽#xff01;大家好#xff0c;我是奇哥#xff0c;一位专门给面试官添堵的职业面试员 文章持续更新#xff0c;可以微信搜索【小奇JAVA面试】第一时间阅读#xff0c;回复【资料】更有我为大家准备的福利哟#xff01; 文章目录 一、题目二、答案三、总结 一、题目
… 哈喽大家好我是奇哥一位专门给面试官添堵的职业面试员 文章持续更新可以微信搜索【小奇JAVA面试】第一时间阅读回复【资料】更有我为大家准备的福利哟 文章目录 一、题目二、答案三、总结 一、题目
给定两个数组 nums1 和 nums2 返回 它们的 交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
示例 1
输入nums1 [1,2,2,1], nums2 [2,2] 输出[2] 示例 2
输入nums1 [4,9,5], nums2 [9,4,9,8,4] 输出[9,4] 解释[4,9] 也是可通过的
二、答案
class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set set1 new HashSet(); Set set2 new HashSet(); for (int num : nums1) { set1.add(num); } for (int num : nums2) { set2.add(num); } return getIntersection(set1, set2); }
public int[] getIntersection(SetInteger set1, SetInteger set2) {if (set1.size() set2.size()) {return getIntersection(set2, set1);}SetInteger intersectionSet new HashSetInteger();for (int num : set1) {if (set2.contains(num)) {intersectionSet.add(num);}}int[] intersection new int[intersectionSet.size()];int index 0;for (int num : intersectionSet) {intersection[index] num;}return intersection;
}}
三、总结 这里关于算法还没有整理完毕文章后面持续更新建议收藏。 文章中涉及到的命令大家一定要像我一样每个都敲几遍只有在敲的过程中才能发现自己对命令是否真正的掌握了。 如果觉得我的文章还不错的话就点个赞吧另外可以微信搜索【小奇JAVA面试】的好文章获取我为大家准备的资料。