LeetCode Algorithm 04_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)).
Tags: Divide and Conquer, Array, Binary Search
分析:
对于数字个数k,如果k为奇数,则k个数的中位数为第(k/2+1)个;对于偶数,中位数为第k/2和(k/2+1)的平均数。
而对于两个有序数组a[m] b[n],找其第i个数的方法是:第一个数组取出前i/2个数,第二个数组则取出前(i-i/2)个数(注意不一定等于i/2),然后比较各自最大的数,如果a[i/2-1]<b[i-i/2-1],则a数组的前i/2个数全是第i个数之前的数,然后从a数组剩下(m-i/2)个数的子数组和b数组中继续取第(i-i/2)个数。
当然,这里可能会出现m<i/2的情形,这时a就取出其前m个数,b取出前i-m个数即可。对称的情况也可能出现在b上面,这时只需要更换下参数顺序即可。
最后说下终止条件,当m全取完即m==0时,就返回b[i-1]。当i/2-1<0即i<2时,无法继续细分(因为细分后要比较a[i/2-1]和b[i-i/2-1]),说明只需要从两个数组中取出第一个,则返回min(a[0],b[0])即可。
c++代码:
class Solution {
public:
double findIth(int A[], int m, int B[], int n, int i) {
if (m > n)
return findIth(B, n, A, m, i);
if (m == ) {
return B[i - ];
}
if (i <= ) {
return min(A[], B[]);
}
int aSeg = min(m, i / ), bSeg = i - aSeg;
if (A[aSeg - ] < B[bSeg - ]) {
return findIth(A + aSeg, m - aSeg, B, n, i - aSeg);
} else if (A[aSeg - ] > B[bSeg - ]) {
return findIth(A, m, B + bSeg, n - bSeg, i - bSeg);
} else {
return A[aSeg - ];
}
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int num = m + n;
if (num % == ) {
return findIth(A, m, B, n, num / + );
} else {
return (findIth(A, m, B, n, num / + )
+ findIth(A, m, B, n, num / )) / ;
}
}
};
LeetCode Algorithm 04_Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- [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 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 【JAVA、C++】LeetCode 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 two ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- [LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...
随机推荐
- Comput_picture
import requestsfrom pyquery import PyQuerycount = 1url = "https://www.169tp.com/diannaobizhi/&q ...
- 今日SGU 5.23
SGU 223 题意:给你n*n的矩形,放k个国王,每个国王不能放在别的国王的8连边上,问你有多少种方法 收获:状态DP,因为每行的放置只会影响下一行,然我们就枚举每行的状态和对应的下一行的状态,当两 ...
- 小米开源便签Notes-源码研究(2)-定时提醒的便签
本篇讲述小米便签中的定时提醒功能. 便签,可以理解为一件事情,一项任务,有个定时提醒的功能,还是蛮不错的~ 小米便签定时功能,是当编辑便签的时候,有个菜单项,选了之后,就弹出一个"日 ...
- or in 、Object.keys()以及Object.getOwnPropertyNames有什么区别?
or in .Object.keys()以及Object.getOwnPropertyNames的区别 var obj= Object.create(parent, { b: { value: 2, ...
- Maven学习总结(19)——深入理解Maven相关配置
MAVEN2的配置文件有两个settings.xml和pom.xml settings.xml:保存的是本地所有项目所共享的全局配置信息,默认在maven安装目录的conf目录下,如果没有安装mave ...
- JBoss配置连接池
什么是数据库连接池? 配置连接池为的是解决效率问题.由于每创建一个连接都是非常耗时的,有了连接池,就能够提前放一些连接进去.以后我们再用连接就去连接池里面取而不是每次都创建.可是我们知道连接池是有上限 ...
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其它方法注入
这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入? 在使用@Inject标签之前.我们须要在pom文件中面增加以下的代码: <depen ...
- C++模板类代码只能写在头文件?
这个问题,实际上我几年前就遇到了.最近写个模板类玩的时候,再次遇到. 当我非常仔细的将定义和实现分开,在头文件中保留了最少的依赖后,一切就绪.cpp单独编过.但是当使用的时候,就会报告所有的函 ...
- VS NuGet使用
通过这个可以自动联网下载内容! 很方便! 工具->NuGet包管理工具->程序包管理器控制台
- 55.npm install 报错 :stack Error: Can't find Python executable "python"
转自:https://www.cnblogs.com/zengry/p/8044379.html 解决方法 : 1. 安装python , 设置环境变量 :cmd --> path='%path ...