-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution349.java
More file actions
34 lines (28 loc) · 785 Bytes
/
Solution349.java
File metadata and controls
34 lines (28 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package algorithm.leetcode;
import java.util.HashSet;
import java.util.Set;
/**
* @author Administrator
* @desc 两个数组的交集
* @create 2019/01/08
*/
public class Solution349 {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>(nums1.length);
Set<Integer> intersect = new HashSet<>(nums2.length);
for (int i = 0; i < nums1.length; ++i) {
set.add(nums1[i]);
}
for (int i = 0; i < nums2.length; ++i) {
if (set.contains(nums2[i])) {
intersect.add(nums2[i]);
}
}
int[] ans = new int[intersect.size()];
int i = 0;
for (Integer n : intersect) {
ans[i++] = n;
}
return ans;
}
}