1. 程式人生 > >349. Intersection of Two Arrays

349. Intersection of Two Arrays

turn cti -o for leetcode etc war tps public

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