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;
}
时间: 2024-08-08 21:53:00