forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1213.java
47 lines (45 loc) · 1.48 KB
/
_1213.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 1213. Intersection of Three Sorted Arrays
*
* 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
* */
public class _1213 {
public static class Solution1 {
/**credit: https://leetcode.com/problems/intersection-of-three-sorted-arrays/discuss/397603/Simple-Java-solution-beats-100*/
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
List<Integer> result = new ArrayList();
int i = 0;
int j = 0;
int k = 0;
while (i < arr1.length && j < arr2.length && k < arr3.length) {
if (arr1[i] == arr2[j] && arr1[i] == arr3[k]) {
result.add(arr1[i]);
i++;
j++;
k++;
} else if (arr1[i] < arr2[j]) {
i++;
} else if (arr2[j] < arr3[k]) {
j++;
} else {
k++;
}
}
return result;
}
}
}