forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode349.java
More file actions
31 lines (22 loc) · 746 Bytes
/
LeetCode349.java
File metadata and controls
31 lines (22 loc) · 746 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
package LeetCode;
import java.util.HashSet;
import java.util.TreeSet;
public class LeetCode349 {
// https://leetcode.com/problems/intersection-of-two-arrays/description/
// 时间复杂度: O(nlogn)
// 空间复杂度: O(n)
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> record = new HashSet<Integer>();
for(int num: nums1)
record.add(num);
HashSet<Integer> resultSet = new HashSet<Integer>();
for(int num: nums2)
if(record.contains(num))
resultSet.add(num);
int[] res = new int[resultSet.size()];
int index = 0;
for(Integer num: resultSet)
res[index++] = num;
return res;
}
}