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 ...
随机推荐
- Angular6 学习笔记——路由详解
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Asp.Net、API 、MVC、 PUT/DELETE 报405错解决办法
本地put/delete能正常运行,但是部署在服务器就 报webapi method not allowed 405的错误 解决方案就是在Web.config里面加入如下设置: <system. ...
- 通过IP获取MAC地址例子(内核层)
博客地址:http://home.cnblogs.com/u/zengjianrong/ 在内核处理此流程,反而更加简单些,代码如下: #include <net/arp.h> #incl ...
- HDU 5047 Sawtooth 找规律+拆分乘
Sawtooth Think about a plane: ● One straight line can divide a plane into two regions. ● Two lines ...
- RDD的转换操作,分三种:单value,双value交互,(k,v)对
import org.apache.spark.rdd.RDDimport org.apache.spark.{Partitioner, SparkConf, SparkContext} object ...
- Prometheus 编写告警规则案例
Prometheus 编写告警规则案例 注:确保alertmanager配置完毕! 1.创建编辑文件:vim /usr/local/prometheus/rules/node.yml # groups ...
- appium+python 多设备并行执行脚本【转】
1.ready.py文件 def getport(): aport = random.randint(4700, 4900) # 判断端口是否被占用 while getDeviceInfo.is_op ...
- SpringBoot security关闭验证
SpringBoot security关闭验证 springboot2.x security关闭验证https://www.cnblogs.com/guanxiaohe/p/11738057.html ...
- Application类-应用程序生命周期
1.创建Application对象 新建WPF程序后,排除掉WPF自动创建的App.xaml,我们自定义一个类,在该类的Main()方法中,创建Application对象,然后调用创建一个窗口对象,最 ...
- Dos.ORM修改数据遇到的问题
2019年11月6日,今天使用Dos.ORM进行数据的批量修改,出现修改一条数据造成所有数据相应状态改变的情况,代码如下: 按照一步步调试的方式,排查出原因:生成的orm实体类缺少 主键 的标识,该原 ...