首页 > 代码库 > 349. Intersection of Two Arrays

349. Intersection of Two Arrays

https://leetcode.com/problems/intersection-of-two-arrays/#/solutions

http://www.cnblogs.com/EdwardLiu/p/6096747.html

三种方法哦哦哦

public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> ans = new HashSet<>();
Set<Integer> set = new HashSet<>();
for (int num : nums1) {
set.add(num);
}
for (int num : nums2) {
if (set.contains(num)) {
ans.add(num);
}
}
int[] res = new int[ans.size()];
int i = 0;
for (Integer num : ans) {
res[i++] = num;
}
return res;
}

技术分享

 

349. Intersection of Two Arrays