-
Notifications
You must be signed in to change notification settings - Fork 23
/
1213. Intersection of Three Sorted Arrays.java
executable file
·103 lines (80 loc) · 2.85 KB
/
1213. Intersection of Three Sorted Arrays.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
E
tags: Hash Table, Two Pointers
time: O(m + n + h) two pointers approach
space: O(1)
Very similar to 349.Intersection of Two Arrays.
#### Hash Table
- Use set to check
- Verify duplicates at end rst
#### Two Pointers
- similar to Intersection of Two Sorted Arrays
- Start from front/back, process 1 item at a time
- if match, move all pointers
- Optoin1: check from back
- Optoin2: check from frotn
```
/*
Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Example 1:
Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8]
Output: [1,5]
Explanation: Only 1 and 5 appeared in the three arrays.
Constraints:
1 <= arr1.length, arr2.length, arr3.length <= 1000
1 <= arr1[i], arr2[i], arr3[i] <= 2000
*/
class Solution {
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
Set<Integer> set1 = toSet(arr1), set2 = toSet(arr2);
List<Integer> rst = new ArrayList<>();
for (int num : arr3) {
if (set1.contains(num) && set2.contains(num)) {
if (rst.size() == 0) rst.add(num);
else if (rst.size() > 0 && rst.get(rst.size() - 1) != num) rst.add(num);
}
}
return rst;
}
private Set<Integer> toSet(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) set.add(num);
return set;
}
}
// Method2: Two Pointers, Optoin1: check from back
class Solution {
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
List<Integer> rst = new LinkedList<>();
int i = arr1.length - 1, j = arr2.length - 1, k = arr3.length - 1;
while (i >= 0 && j >= 0 && k >= 0) {
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
if (rst.isEmpty() || arr1[i] != rst.get(rst.size() - 1)) rst.add(0, arr1[i]);
i--;
j--;
k--;
} else if (arr2[j] < arr3[k]) k--;
else if (arr1[i] < arr2[j]) j--;
else i--;
}
return rst;
}
}
// Method2: Two Pointers, Optoin2: check from front
class Solution {
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
List<Integer> rst = new LinkedList<>();
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length && k < arr3.length) {
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
if (rst.isEmpty() || arr1[i] != rst.get(rst.size() - 1)) rst.add(arr1[i]);
i++;
j++;
k++;
} else if (arr1[i] < arr2[j]) i++;
else if (arr2[j] < arr3[k]) j++;
else k++;
}
return rst;
}
}
```