问题: 给你两个排序的数组,求两个数组的交集。

比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5,n是a数组大小,m是b数组大小。

思路:

(1)从b数组遍历取值,然后把值与a数组的每一个值进行比较,如果有相等的,就保存下来,直到ab全部遍历完,这样时间复杂度就是O(nm)。

(2)把上面的改进一下,我们在把b里面的值与a比较时,我们采取二分搜索的方式(因为数组都是有序的),这样的话时间复杂度就会变为O(mlogn),如果a数组更小的话,我们会取a数组的值去和b比较,这样的话就是O(nlogm)。

(3)要做到O(n+m),上面两种显然是不可以的,这时我们采取双指针的方式,因为数组A B均排过序,所以,我们可以用两个“指针”分别指向两个数组的头部,如果其中一个比另一个小,移动小的那个数组的指针;如果相等,那么那个值是在交集里,保存该值,这时,同时移动两个数组的指针。一直这样操作下去,直到有一个指针已经超过数组范围。

代码如下:

public List<Integer> intersection(int[] A, int[] B) {
if (A == null || B == null || A.length == 0 || B.length == 0)
return null;
ArrayList<Integer> list = new ArrayList<Integer>();
int pointerA = 0;
int pointerB = 0;
while (pointerA != A.length && pointerB != B.length) {
if (A[pointerA] > B[pointerB]) {
pointerB++;
} else if (A[pointerA] < B[pointerB]) {
pointerA++;
} else {
list.add(A[pointerA]);
pointerA++;
pointerB++;
}
}
return list;
}

并集代码如下:

public List<Integer> union(int[] A, int[] B) {
if (A == null || B == null || A.length == 0 || B.length == 0)
return null;
ArrayList<Integer> list = new ArrayList<Integer>();
int pointerA = 0;
int pointerB = 0;
while (pointerA < A.length && pointerB < B.length) {
if (A[pointerA] > B[pointerB]) {
list.add(B[pointerB]);
pointerB++;
} else if (A[pointerA] < B[pointerB]) {
list.add(A[pointerA]);
pointerA++;
} else {
list.add(A[pointerA]);
pointerA++;
pointerB++;
}
}
// 退出来之后,把剩余的继续添加
while (pointerB <= B.length - 1) {
list.add(B[pointerB]);
pointerB++;
}
while (pointerA <= A.length - 1) {
list.add(A[pointerA]);
pointerA++;
}
return list;
}

  

求两个排序数组的交集和并集----时间复杂度O(n+m)的更多相关文章

  1. 求两个排序数组中位数 C++

    题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nu ...

  2. 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 ...

  3. Leetcode4--->求两个排序数组的中位数

    题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...

  4. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  5. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  6. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  7. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  8. 取两个String数组的交集

    import org.testng.annotations.Test; import java.util.HashMap; import java.util.LinkedList; import ja ...

  9. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

随机推荐

  1. QT开发小技巧-窗口处理(一)

    this->setWindowFlags(Qt::WindowCloseButtonHint); // 仅保留关闭按钮 this->setAttribute(Qt::WA_DeleteOn ...

  2. vscode 显示 Module 'turtle' has no … member

    初次运行与 turtle 相关的 Python 代码时,vscode 上显示 Module 'turtle' has no - member. 这时,我们可以在 vscode 的设置里添加如下代码: ...

  3. 函数——es6函数扩展(二)

    一.声明 1. let(变量) 可以只声明不给值(默认为undefined),或者是先声明后给值,但是必需声明后再使用,可以重复赋值,可以防止变量泄露: 同一作用域里不能重复的声明,不同作用域里可以, ...

  4. asp.net ListView控件的简单实用和配置

    1 web窗体界面代码 ItemType:控件要绑定的实体模型 SelectMethod:控件获取实体集合的后台方法 DataKeyNames:实体的主键 UpdateProduct:设置跟新的方法 ...

  5. Spring Data JPA 大纲归纳

    第一天: springdatajpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查 ...

  6. phpstorm 习惯设置

    phpstorm 习惯设置 1. 字体:Source Code Pro 大小:14 链接: https://pan.baidu.com/s/1HLpbduBHFvbq1a10QV4uCg 提取码: y ...

  7. 11_Hive TransForm 案例

    1.需求:将Json格式的数据处理后插入新表中 数据文件如下:rating.json,文件格式:{"movie":"2858","rate" ...

  8. Vue入门(二)——Demo

    1.在工程目录下创建项目 右键GIT BASH 2.安装lib(建议使用淘宝镜像安装) 3.首页 App.vue <template> <el-container> <e ...

  9. python_函数参数

    1.参数的基本知识 任意个数 任意类型 def func(a1,a2,a3): print(a1,a2,a3) # 参数可以是任意个数和任意类型 func(1,'waf',True) 2.位置传参数( ...

  10. Hosts 文件切换工具

    建议删除无用的注释,否则在启用全部的时候会把注释和说明内容的注释取消掉 下载地址: