-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersection.java
More file actions
57 lines (47 loc) · 1.45 KB
/
Intersection.java
File metadata and controls
57 lines (47 loc) · 1.45 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Intersection {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1.length == 0 || nums2.length == 0) {
return new int[0];
}
List<Integer> retList = new ArrayList<>();
// Sort both arrays
Arrays.sort(nums1);
Arrays.sort(nums2);
int ptr1 = 0;
int ptr2 = 0;
while (true) {
int val1 = nums1[ptr1];
int val2 = nums2[ptr2];
if (val1 == val2 && !retList.contains(val1)) {
retList.add(val1);
}
if (val1 < val2 && ptr1 < nums1.length - 1) {
ptr1++;
} else if (ptr2 < nums2.length - 1) {
ptr2++;
} else {
if (ptr1 < nums1.length - 1) {
ptr1++;
} else if (ptr2 < nums2.length - 1) {
ptr2++;
} else {
break;
}
}
}
return retList.stream().mapToInt(i->i).toArray();
}
public void run() {
// int[] nums1 = {4, 9, 5};
// int[] nums2 = {9, 4, 9, 8, 4};
// int[] nums1 = {1, 2, 2, 1};
// int[] nums2 = {2, 2};
int[] nums1 = {2, 1};
int[] nums2 = {1, 2};
int[] intersect = intersection(nums1, nums2);
System.out.println(Arrays.toString(intersect));
}
}