原题链接在这里:https://leetcode.com/problems/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.

Constraints:

  • 1 <= arr1.length, arr2.length, arr3.length <= 1000
  • 1 <= arr1[i], arr2[i], arr3[i] <= 2000

题解:

Have 3 pointers pointing to 3 arrays.

If 3 pointed values are the same, add it to res and move all 3 pointers.

Else if first value < second value, move 1st pointer.

Else if second value < third value, now first value must be >= second value, need to move 2nd pointer.

Else move the 3rd pointer, since noew first value >= second value and second value >= third value.

Time Complexity: O(n). n = sum of array lengths.

Space: O(1).

AC Java:

 class Solution {
public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
List<Integer> res = new ArrayList<>();
if(arr1 == null || arr2 == null || arr3 == null){
return res;
} 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]){
res.add(arr1[i]);
i++;
j++;
k++;
}else if(arr1[i] < arr2[j]){
i++;
}else if(arr2[j] < arr3[k]){
j++;
}else{
k++;
}
} return res;
}
}

类似Intersection of Two Arrays.

LeetCode 1213. Intersection of Three Sorted Arrays的更多相关文章

  1. 【leetcode】1213.Intersection of Three Sorted Arrays

    题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...

  2. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  3. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  4. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  5. LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  6. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  7. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

  8. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

  9. [LeetCode] 4. Median of Two Sorted Arrays ☆☆☆☆☆

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. Oracle体系结构学习笔记

    Oracle体系结构由实例和一组数据文件组成,实例由SGA内存区,SGA意思是共享内存区,由share pool(共享池).data buffer(数据缓冲区).log buffer(日志缓冲区)组成 ...

  2. Java中调用存储过程

    dao层: import java.util.Map; public interface AppGthdDao { public String gthd(Map map); } mapper层 < ...

  3. Java学习:方法的引用

    方法引用(Method references) lambda表达式允许我们定义一个匿名方法,并允许我们以函数式接口的方式使用它.我们也希望能够在已有的方法上实现同样的特性. 方法引用和lambda表达 ...

  4. C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装

    基于Oracle.ManagedDataAccess.Client封装的Oracle工具类OracleHelper,代码如下: using System; using System.Data; usi ...

  5. git tag 常用笔记

    git tag 常用笔记 查看 tag 列出现有 tag git tag 列出 v1.4.2 相关的 tag git tag -l "v1.4.2" 查看指定 tag 的信息 gi ...

  6. web.xml——安全性框架配置文件

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  7. 2019 海看java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.海看等公司offer,岗位是Java后端开发,因为发展原因最终选择去了海看,入职一年时间了,也成为了面试官,之 ...

  8. 二、NodeJS入门——准备工作(2)——MongoDB安装以及客户端Robomongo安装和使用

    目录     1.介绍     2.下载地址     3.MongoDB安装过程     4.MongoDB的使用     5.MongoDB添加管理员账户     6.RoboMongo安装过程   ...

  9. [转]C++ 堆栈溢出的原因以及可行的解决方法

    栈溢出(stackoverflow)的原因及解决办法 大家都知道,Windows程序的内存机制大概是这样的:全局变量(局部的静态变量本质也属于此范围)存储于堆内存,该段内存较大,一般不会溢出: 函数地 ...

  10. android studio学习---菜单栏BUILD功能

    项目构建: 1.构建项目 2.重构项目 3.签名打包