求两个排序数组的交集和并集----时间复杂度O(n+m)
问题: 给你两个排序的数组,求两个数组的交集。
比如: 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)的更多相关文章
- 求两个排序数组中位数 C++
题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nu ...
- 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 ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
- LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...
- 取两个String数组的交集
import org.testng.annotations.Test; import java.util.HashMap; import java.util.LinkedList; import ja ...
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
随机推荐
- 查找最大和次大元素(JAVA版)(分治法)
问题描述:对于给定的含有n个元素的无序序列,求这个序列中最大和次大的两个不同元素. 问题求解分析(分治法):先给出无序序列数组a[low...high].第一种情况为当数组中只有一个元素时,此时只存在 ...
- IOS 点击按钮拨号
- (IBAction)OnTouch_bHotLine:(id)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithS ...
- MySQL自测测试
#建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...
- JS ES6
变量 let 块级作用域内有效 不能重复声明 不会预处理,不存在提升 var btns = document.getElementsByTagName('button'); for (let i = ...
- [转]关闭ssh的自动启动
转载自:http://blog.chinaunix.net/uid-20147410-id-3206364.html 安装了ssh服务,但是不希望他开机自动启动,可以如下设置: 在/etc/init/ ...
- Jmeter服务器压力测试使用说明
Jmeter服务器压力测试使用说明 Apache JMeter是Apache组织开发的基于Java的压力测试工具. 官方地址:http://jmeter.apache.org/download_jme ...
- JavaSpring【七、AspectJ】
AspectJ 概念 @AspectJ类似纯Java注解的普通Java类 Spring可以使用AspectJ来作为切入点 AOP在运行时仍是纯SpringAOP,对AspectJ无依赖 配置: 对@A ...
- vue-element-admin 之设置侧边栏的icon
一.将icon的svg文件放置如下图文件位置 二.在路由设置中设置(icon设置为svg的文件名称即可) 注:若icon选中的颜色不会随着侧边栏文字颜色变动,把svg文件中的fill ...
- Educational Codeforces Round 40 C. Matrix Walk( 思维)
Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...
- 如何通过字符串形式导包(importlib模块的使用)
1 模块简介 Python提供了importlib包作为标准库的一部分.目的就是提供Python中import语句的实现(以及__import__函数).另外,importlib允许程序员创建他们自定 ...