共有三种思路。

哈希表。

将较小的那个数组中的所有元素存在哈希表中。然后依次验证另一个数组中的数字是否有出现过。时间复杂度O(m + n),空间复杂度O(min(m, n))

二分搜索法

将较小的那个数组中的每一个元素,都用二分搜索的方法在较大数组中验证是否出现过。当两个数组大小相差很大时,这种方法会很快。

时间复杂度O(min(mlogn, nlogm)), 空间复杂度O(1)。

两个指针

指针i指向一个数组,指针j指向另一个数组。如果i < j,则i++;如果j < i,则j++;如果i = j,则将这个数存入结果,并将两个指针都加一。

时间复杂度O(m + n), 空间复杂度O(1)。

Find intersection of two sorted arrays的更多相关文章

  1. LeetCode 1213. Intersection of Three Sorted Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-three-sorted-arrays/ 题目: Given three integer a ...

  2. 【leetcode】1213.Intersection of Three Sorted Arrays

    题目如下: Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a s ...

  3. algorithm@ find kth smallest element in two sorted arrays (O(log n time)

    The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...

  4. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  5. [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 ...

  6. 【leetcode】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 s ...

  7. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  8. leetcode-【hard】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 ...

  9. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. python的inspect模块

    一.type and members 1. inspect.getmembers(object[, predicate]) 第二个参数通常可以根据需要调用如下16个方法: 返回值为object的所有成 ...

  2. 5.修改haproxy配置文件

    需求: 1.查 输入:www.oldboy.org 获取当前backend下的所有记录 2.新建 输入: arg = { 'backend': 'www.oldboy.org', 'record':{ ...

  3. coreos ipa image Injection of public key

    查看readme To embed the oem/ directory into a CoreOS pxe image:   Note: In order to have the ability t ...

  4. shell之dialog提示窗口

    dialog 提示窗口 1.msgbox     dialog --msgbox text 20 10 2.yesno     dialog --title "Please answer&q ...

  5. html页面分块加载

    方法:jQuery ajax - load() 方法 这个方法可以请求html页面,并把结果放在指定元素内.

  6. hdu 1551 Cable master (二分法)

    Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. Strassen优化矩阵乘法(复杂度O(n^lg7))

    按照算法导论写的 还没有测试复杂度到底怎么样 不过这个真的很卡内存,挖个坑,以后写空间优化 还有Matthew Anderson, Siddharth Barman写了一个关于矩阵乘法的论文 < ...

  8. codeforces school mark(贪心)

    ///太渣,看了题解才知道怎么做,自己想感觉想不清楚 ///题解:首先在给出的序列里判断小于median的个数,若大于(n-1)/2,则不满足,否则看另一个条件 ///这样我们可以把中位数左边还要添加 ...

  9. android的apk文件结构

    什么是APK?APK文件都由那些组成?不懂没关系,让小编来为你详细解答. 一.APK简介与描述 APK是AndroidPackage的缩写,即Android安装包(apk).APK是类似Symbian ...

  10. macOS Sierra下如何打开任何来源(10.12系统)

    转载声明:本站文章无特别说明皆为原创,转载请注明:史蒂芬周的博客, 一定有很多朋友和小子一样,迫不及待的升级到了macOS Sierra,随之而来的是第三方应用都无法打开了,提示无法打开或者扔进废纸篓 ...