-
Notifications
You must be signed in to change notification settings - Fork 457
/
Solution.java
41 lines (32 loc) · 1.05 KB
/
Solution.java
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
35
36
37
38
39
40
41
import java.util.TreeSet;
// 349. Intersection of Two Arrays
// https://leetcode.com/problems/intersection-of-two-arrays/description/
// 时间复杂度: O(nlogn)
// 空间复杂度: O(n)
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
TreeSet<Integer> record = new TreeSet<Integer>();
for(int num: nums1)
record.add(num);
TreeSet<Integer> resultSet = new TreeSet<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;
}
private static void printArr(int[] arr){
for(int e: arr)
System.out.print(e + " ");
System.out.println();
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 2, 1};
int[] nums2 = {2, 2};
int[] res = (new Solution()).intersection(nums1, nums2);
printArr(res);
}
}