LeetCode 1213. Intersection of Three Sorted Arrays

原题链接在这里: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:

 1 class Solution {
 2     public List<Integer> arraysIntersection(int[] arr1, int[] arr2, int[] arr3) {
 3         List<Integer> res = new ArrayList<>();
 4         if(arr1 == null || arr2 == null || arr3 == null){
 5             return res;
 6         }
 7
 8         int i = 0;
 9         int j = 0;
10         int k = 0;
11         while(i < arr1.length && j < arr2.length && k < arr3.length){
12             if(arr1[i] == arr2[j] && arr1[i] == arr3[k]){
13                 res.add(arr1[i]);
14                 i++;
15                 j++;
16                 k++;
17             }else if(arr1[i] < arr2[j]){
18                 i++;
19             }else if(arr2[j] < arr3[k]){
20                 j++;
21             }else{
22                 k++;
23             }
24         }
25
26         return res;
27     }
28 }

类似Intersection of Two Arrays.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12047372.html

时间: 2024-10-11 21:39:17

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

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Finding intersection of two sorted arrays

Find the intersection of two sorted arrays. Let’s called array1 as A and array2 as B, each with size m and n. The obvious brute-force solution is to scan through each element in A, and for each element in A, scan if that element exist in B. The runni

LeetCode OJ - Median of Two Sorted Arrays

题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数.所以只要解决了第k小数的问题,原问题也得以解决

[LeetCode][Python]Median of Two Sorted Arrays

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.The overall run time complexity should be O(log (m+n)).

[LeetCode][JavaScript]Median of Two Sorted Arrays

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 sorted arrays. The overall run time complexity should be O(log (m+n)). https://leetcode.com/problems/median-of-two-sorted

LeetCode题解-----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 sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们

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 sorted arrays. The overall run time complexity should be O(log (m+n)). 题意: 两个排序后的数组nums1 和nums2,长度分别是m,n,找出其中位数,并且时间复杂度:O(log(m+n)) 最愚蠢的方法: 两个数组合

LeetCode #4 Median of Two Sorted Arrays (H)

[Problem] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). [Analysis] 由于两个数组已经是有序的,这题考的是如何将Linear复杂度优化到Log复杂度.那么很容易就能想到二分法或者说分治