Ai
1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_1213.java 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 2019-11-29 01:20 +08:00 . refactor 1213
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;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助