LeetCode 1213. Intersection of Three Sorted Arrays
原题链接在这里:https://leetcode.com/problems/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 <= 10001 <= 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;
}
}
LeetCode 1213. Intersection of Three Sorted Arrays的更多相关文章
- 【leetcode】1213.Intersection of Three Sorted Arrays
题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- [LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...
- [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 ...
随机推荐
- Azure Devops (VSTS) Extensions 开发小记
我在使用tfx-cli打包Azure Devops插件时,输出了很黄很黄很亮瞎眼的(尤其是在Visual Studio Code采用了Dark Black Theme的情况下)警告warning: P ...
- 批量插入sql技巧
方式一: ); ); 方式二: ), (); 第二种比较好.第二种的SQL执行效率高的主要原因是合并后日志量(MySQL的binlog和innodb的事务让日志)减少了,降低日志刷盘的数据量和频率,从 ...
- python3 四舍五入(0.5可以进1)
今天做了一个题要求四舍五入,然后找了一个方法:round()可以四舍五入, 试了试1.5--->2 试了试0.5--->0 !!!! 找了几个方法说可以的: # 方法一: from _ ...
- Laravel框架中Echo的使用过程
今天的这篇文章中给大家分享关于laravel框架中echo的使用,文章的内容是一步一步来的,用了16步走完一个过程,过程很清晰,希望可以帮助到有需要的朋友吧.话不多说,直接看内容.官方文档推荐使用 P ...
- Scratch 3.6环境搭建(万江波实战记录)
1.Scratch官网在线环境 官方网址:scratch.mit.edu 进入后,点击“Create”建立 2. Scratch官网在线环境_简体中文(点击这个地球) 3-选择:创意 4-进入”创意“ ...
- CI/CD之Gitlab集成Jenkins多分支pipeline实现质量检测和自动发布
本次实施主要实现: 代码提交gitlab,自动触发Jenkins构建 gitlab发起Merge Request, 需要Jenkins检查通过才可以merge,实现代码review和质量管控 gitl ...
- 深入学习OpenCV中图像灰度化原理,图像相似度的算法
最近一段时间学习并做的都是对图像进行处理,其实自己也是新手,各种尝试,所以我这个门外汉想总结一下自己学习的东西,图像处理的流程.但是动起笔来想总结,一下却不知道自己要写什么,那就把自己做过的相似图片搜 ...
- 【Elasticsearch】【WEB】java web服务连接es elasticsearch始终报错,无法正常连接使用的错误解决历程
前情提要: web服务往华为云上迁移 ================内网的好环境,相关配置=================== 1.web服务关于ES的集群配置如下: elasticAddress ...
- LinkedHashMap 的核心就 2 点,搞清楚,也就掌握了
HashMap 有一个不足之处就是在迭代元素时与插入顺序不一致.而大多数人都喜欢按顺序做某些事情,所以,LinkedHashMap 就是针对这一点对 HashMap 进行扩展,主要新增了「两种迭代方式 ...
- 微信开放平台apk的应用签名的获取
https://open.weixin.qq.com 微信里面的应用签名相关的签名信息 1.首先生成JKS文件,放入的是包名,利用的是android studio工具生成的. 步骤:随便建立一个安卓项 ...