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 <= 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;
}
}
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 ...
随机推荐
- 记录webservice
公司的一个老项目,定义了接口,供其他应用访问.定义的方式就是webservice. 我这边的环境是springboot. 首先引入依赖jar 声明一个服务端. @WebSerevice注解中name则 ...
- CSS3做动物走路效果
CSS3做动物走路效果 采用的CSS3切换序列帧做 核心代码如下<pre>.game .role { width: 60px; height: 86px; position: absolu ...
- 浙大版《C语言程序设计(第3版)》题目集 --总结
浙大版<C语言程序设计(第3版)>题目集 此篇博客意义为总结pta上浙大版<C语言程序设计(第3版)>题目集所做题目的错误点,心得体会. 1.练习2-10 计算分段函数[1] ...
- 百度前端技术学院task1 总结
1.居中:当使用text-align或者vatical-align无法达到居中的时候,如果知道元素的大小,可以采用先设为left或right为50%,再设置margin-left或者margin-ri ...
- 【题解】宫廷守卫 [P1263]
[题解]宫廷守卫 [P1263] 传送门:宫廷守卫 \([P1263]\) [题目描述] 给出一个 \(n*m\) 的方格图,分别用整数 \(0,1,2\) 表示空地.陷阱.墙,空地上可以放置守卫,如 ...
- 一段简单的顶部JS广告
一段简单的顶部JS广告 <SCRIPT LANGUAGE="JavaScript"> ; ; images = new Array; images[] = new Im ...
- C# 打开mpp文件(Microsoft object)问题总结
有需求就有解决方案,早上还没有听说过什么是 mpp 文件,下午已经能成功的将功能实现,这难道就是程序员的职业素养?哈哈哈哈 从网上找了很多方法,最后自己找到一个十分简单的打开 mpp 文件的方法: p ...
- Asp.Net Core中使用NLog记录日志
2019/10/28, Asp.Net Core 3.0, NLog 4.6.7, NLog.Web.AspNetCore 4.9.0 摘要:NLog在asp.net网站中的使用,NLog日志写入数据 ...
- NetCoreApi框架搭建三、AutoFac 依赖入注)
这里不多做理论上的解释,因为我感觉自己也不是很完全的理解,所以只是记录我自己做的过程. 首先还是粘贴大神的链接:https://www.cnblogs.com/RayWang/p/11165509.h ...
- Java开发环境系列:一篇能解决你99%问题的排雷日记
安装 https://archive.apache.org/dist/tomcat/ 推荐使用免安装版的Tomcat(放在没有中文和空格的目录下),前提是已经安装了JDK并配置了环境变量.Linu ...