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 ...
随机推荐
- hdu 5375 - Gray code(dp) 解题报告
Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- nagios,zabbix对照
nagios/zabbix对照: nagios核心功能是监控报警.是一个轻量化的监控系统. 假设须要图标显示,须要添加图标显示插件(如pnp4nagios): 假设须要存入数据库,须要对应的插件(ND ...
- RadioButton的check改变的时候
https://stackoverflow.com/questions/8095256/asp-net-radio-button-change You'll need to specify the a ...
- Fragment-管理Fragment2
上一篇,给大家讲了有关Fragment管理的几个函数,即add,replace,remove,这节再讲讲其它函数,然后再给大家看一个系统BUG. 一.hide().show() 1.基本使用 这两个函 ...
- 分享一个jquery实现的双向选择组件
<html><head> <meta charset="utf-8"> <title>数据删选组件</title> &l ...
- Java:异常体系
异常的类别:可处理异常,运行时异常,非运行时异常 子类重写父类方法,父类方法有异常抛出, 子类重写父类的方法? 不能比父类抛出更大的异常 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?ca ...
- Pig源代码分析: 简析运行计划的生成
摘要 本文通过跟代码的方式,分析从输入一批Pig-latin到输出物理运行计划(与launcher引擎有关,通常是MR运行计划.也能够是Spark RDD的运行算子)的总体流程. 不会详细涉及AST怎 ...
- Day6下午题解1
预计分数:100+?+30=130+? 实际分数:100+25+30=155 T1 https://www.luogu.org/problem/show?pid=T15920 DP裸题,用dp[i][ ...
- golang sync.Mutex
//go func 和主线程之间的关系是并行和竞争关系 package main import ( "fmt" "sync" "time" ...
- BZOJ1009: [HNOI2008]GT考试(KMP+矩阵乘法)
Description 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0< ...