1213. Intersection of Three Sorted Arrays

Given three integer arrays arr1arr2 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.

Example 2:

Input: arr1 = [197,418,523,876,1356], arr2 = [501,880,1593,1710,1870], arr3 = [521,682,1337,1395,1764]
Output: []

 1 class Solution {
 2     public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
 3         int a = 0, b = 0, c = 0;
 4         List<Integer> res = new ArrayList<>();
 5         while (a < arr1.length && b < arr2.length && c < arr3.length) {
 6             int min = Math.min(arr1[a], Math.min(arr2[b], arr3[c]));
 7             if (arr1[a] == min && arr2[b] == min && arr3[c] == min) res.add(min);
 8             if (arr1[a] == min) a++;
 9             if (arr2[b] == min) b++;
10             if (arr3[c] == min) c++;
11         }
12         return res;
13     }
14 }

 

posted @ 2021-03-13 14:26  北叶青藤  阅读(66)  评论(0编辑  收藏  举报