0004. 寻找两个有序数组的中位数(Java)
4. 寻找两个有序数组的中位数
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

最简单的就是用最简单的,把两个数组分别抽出然后排成一个排好序的数组,然后根据中位数的定义,直接根据中间的索引值得到中位数的值。
如果上面这么说明有些抽象的话,我们来看看代码:
Show the Code.
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] all = new int[nums1.length + nums2.length];
int index1 = 0;
int index2 = 0;
int allIndex = 0;
int len1 = nums1.length;
int len2 = nums2.length;
while (index1 < len1 && index2 < len2) {
if (nums1[index1] <= nums2[index2]) {
all[allIndex] = nums1[index1];
index1++;
} else {
all[allIndex] = nums2[index2];
index2++;
}
allIndex++;
}
while (index1 < len1) {
all[allIndex] = nums1[index1];
index1++;
allIndex++;
}
while (index2 < len2) {
all[allIndex] = nums2[index2];
index2++;
allIndex++;
}
int midIndex = (len1 + len2) / 2;
if ((len1 + len2) % 2 == 0) {
return (all[midIndex - 1] + all[midIndex]) / 2.0;
} else {
return all[midIndex];
}
}
}
以上的时间复杂度就是$O(N+M)$,但是这肯定不是最好的做法,最好的做法,时候来看了答案的解析才明白的,不得不说思想的巧妙,真的是要多看几遍。这里放上连接吧
这里就贴一下代码。
https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei-shu-b/
class Solution {
public double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
if (m > n) { // to ensure m<=n
int[] temp = A; A = B; B = temp;
int tmp = m; m = n; n = tmp;
}
int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
while (iMin <= iMax) {
int i = (iMin + iMax) / 2;
int j = halfLen - i;
if (i < iMax && B[j-1] > A[i]){
iMin = i + 1; // i is too small
} else if (i > iMin && A[i-1] > B[j]) {
iMax = i - 1; // i is too big
} else { // i is perfect
int maxLeft = 0;
if (i == 0) { maxLeft = B[j-1]; }
else if (j == 0) { maxLeft = A[i-1]; }
else { maxLeft = Math.max(A[i-1], B[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; }
int minRight = 0;
if (i == m) { minRight = B[j]; }
else if (j == n) { minRight = A[i]; }
else { minRight = Math.min(B[j], A[i]); }
return (maxLeft + minRight) / 2.0;
}
}
return 0.0;
}
}
时间复杂度就是$O(log(N+M))$
其实二分查找有个很重要就是要找出三个要素:
1 达到退出的条件: 这个就比较复杂了,要根据情况而定
2 把区间缩小的条件: 这个就是有定式的,一般都是max =i-1或者min = i + 1
3 修改中位值的操作:mid = (left + (right - left)>>1)
写出最基础的二分查找最基本的操作,但是真正可以完全理解二分思想,那又是另一个级别的故事了。
0004. 寻找两个有序数组的中位数(Java)的更多相关文章
- LeetCode Golang 4. 寻找两个有序数组的中位数
4. 寻找两个有序数组的中位数 很明显我偷了懒, 没有给出正确的算法,因为官方的解法需要时间仔细看一下... func findMedianSortedArrays(nums1 []int, nums ...
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- Java实现 LeetCode 4 寻找两个有序数组的中位数
寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 n ...
- 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- leetcode题目4.寻找两个有序数组的中位数(困难)
题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 ...
- leetcode刷题四<寻找两个有序数组的中位数>
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- [LeetCode] 4. 寻找两个有序数组的中位数
题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 ...
- 【LeetCode】4. 寻找两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- Leetcode题库——4.寻找两个有序数组的中位数
@author: ZZQ @software: PyCharm @file: findMedianSortedArrays.py @time: 2018/10/10 19:24 说明:给定两个大小为 ...
随机推荐
- 创建服务消费者(Feign)
概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...
- "犯罪心理"解读Mybatis拦截器
原文链接:"犯罪心理"解读Mybatis拦截器 Mybatis拦截器执行过程解析 文章写过之后,我觉得 "Mybatis 拦截器案件"背后一定还隐藏着某种设计动 ...
- .NET Core IdentityServer4实战 第Ⅳ章-集成密码登陆模式
回顾下ClientCredentials模式,在ReSourceApi中定义了我们公开服务,第三方网站想要去访问ReSourceApi则需要在身份验证服务中获取toekn,根据token的内容,硬编码 ...
- tomcat实现
转载地址:https://blog.csdn.net/u014795347/article/details/52328221?locationNum=2&fps=1 以下代码纯属本人复制,而且 ...
- python 基本数据类型之整数和布尔值
#1. 当前整数的二进制表示,以最少位数 # age = # print(age.bit_length()) #2. 获取当前数据的字节表示 # age = # v = age.to_bytes(,b ...
- Cisco packet tracer下dhcp的配置的vlan的应用
话不多说,先上拓扑图. pc0和pc1分别接在三层交换机的F0/1.F0/2接口,ser接在F0/24接口,用ser用作dhcp的服务器. 0x01:配置server0 先配置server的IP地址. ...
- POJ 1966:Cable TV Network(最小点割集)***
http://poj.org/problem?id=1966 题意:给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. 思路:将点i拆成a和b,连一条a-&g ...
- BZOJ 2039:[2009国家集训队]employ人员雇佣(最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=2039 题意:中文题意. 思路:一开始想着和之前做的最大权闭合图有点像,但是如果把边全部当成点的话,那 ...
- iOS组件化开发一远程私有库的升级(三)
一.远程私有库的升级 1. 把新增的类 拖入到 classes 文件夹中 : 2. 修改 pod.spec 文件的 s.verson = ‘0.2.0': 二.更新远程仓库 1.cd 到本地仓库的位置 ...
- intel FPGA CLKn pin 是否能直接进PLL?
原创 by DeeZeng FPGA的时钟需要从专用的时钟管脚输入,那CLKn 作为Single-End时钟pin时是否能直接进 PLL呢? 通过查看对应FPGA型号的手册,得出以下结论 1. Cyc ...