Skip to content

Commit aaedbe9

Browse files
add one solution for 1213
1 parent b004e04 commit aaedbe9

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/java/com/fishercoder/solutions/_1213.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
35
import java.util.List;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
48

59
/**
610
* 1213. Intersection of Three Sorted Arrays
@@ -21,7 +25,19 @@
2125
public class _1213 {
2226
public static class Solution1 {
2327
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
24-
return null;
28+
List<Integer> result = new ArrayList<>();
29+
Set<Integer> set2 = Arrays.stream(arr2).boxed().collect(Collectors.toSet());
30+
Set<Integer> set3 = Arrays.stream(arr3).boxed().collect(Collectors.toSet());
31+
for (int i = 0; i < arr1.length; i++) {
32+
if (i >= arr2.length || i >= arr3.length) {
33+
break;
34+
} else {
35+
if (set2.contains(arr1[i]) && set3.contains(arr1[i])) {
36+
result.add(arr1[i]);
37+
}
38+
}
39+
}
40+
return result;
2541
}
2642
}
2743
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1213;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _1213Test {
9+
private static _1213.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _1213.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
CommonUtils.printList(solution1.arraysIntersection(new int[]{1, 2, 3, 4, 5}, new int[]{1, 2, 5, 7, 9}, new int[]{1, 3, 4, 5, 8}));
19+
}
20+
21+
}

0 commit comments

Comments
 (0)