代码拉取完成,页面将自动刷新
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;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。