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 ...
随机推荐
- 第25课 std::thread对象的析构
一. 线程的等待与分离 (一)join和detach函数 1. 线程等待:join() (1)等待子线程结束,调用线程处于阻塞模式. (2)join()执行完成之后,底层线程id被设置为0,即join ...
- GNU Wget 1.14 用法
GNU Wget 1.14,非交互式的网络文件下载工具.用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version ...
- 使用Windows的Linux子系统搭建嵌入式开发环境
亲,都9102年了,还在用VMware跑嵌入式交叉编译链吗? 北京时间2019年6月13日,Windows 10发布预览版本18917.版本的主要功能是Linux子系统(windows sub ...
- Linux命令随手记
随手记录常用的Linux命令. tar 解压. tar -xzvf tar 压缩:tar -czvf .tgz (z是压缩格式,x为解压,v为显示过程,f指定备份文件) tar -zc ...
- Spring69道面试题
Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...
- python 属性描述符
import numbers class IntField: # 一个类只要实现了这个魔法函数,那么它就是属性描述符 #数据描述符 def __get__(self, instance, owner) ...
- @Bean修饰的方法参数的注入方式
@Bean修饰的方法参数的注入方式: 方法参数默认注入方式为Autowired,即先根据类型匹配,若有多个在根据名称进行匹配. 1:复杂类型可以通过@Qualifier(value=“XXX”)限定; ...
- 五、Hexo静态博客背景及界面显示优化配置
示例预览:我的主页 背景图片添加 自动切换背景 静态本地背景 首先将已选定的背景图片放到博客根目录下的\source\images下 示例:D:\Blog\source\images\backgr ...
- C#工具类OracleHelper,基于Oracle.ManagedDataAccess.Client封装
基于Oracle.ManagedDataAccess.Client封装的Oracle工具类OracleHelper,代码如下: using System; using System.Data; usi ...
- el-upload进度条无效,on-progress无效问题解决方案
事先声明,本人系.net后端老菜鸟,vue接触没有多长时间,如果存在技术分享错误,切莫见怪,第一次写博,还请大佬们多多担待,转载请注明出处谢谢! 最近项目用到饿了么上传,于是参照官网接入el-uplo ...