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的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

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

  4. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

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

  6. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

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

  8. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

  9. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

随机推荐

  1. CodeVS 1296 营业额统计

    1296 营业额统计2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description Tiger最近被公司升任为营业部经理, ...

  2. 数据库SQL Server2012笔记(四)——多表查询、子查询、分页查询、用查询结果创建新表和外连接

    1.多表查询 1)笛卡尔集: select  *  from  表名1,表名2 select  *  from  表名1.表名2  where   表名1.字段名=表名2.字段名 注: 若有两张表有同 ...

  3. ios in-house 公布整个过程(startssl认证)

    首先大体说一下步骤: 1.申请苹果enterprise 账号 为应用生成app id,provision profile等 详见:http://www.th7.cn/Program/IOS/20131 ...

  4. H.264视频编解码SoC满足高清DVR设计需求

    硬盘录像机(DVR)作为监控系统的核心部件之一,在10年里高速发展,从模拟磁带机的替代品演变成具有自己独特价值的专业监控数字平台,并被市场广泛接受.监控系统伴随DVR这些年的发展向着IP化.智能化发展 ...

  5. 30.IntellJ Idea 导入已存在的Maven项目

    转自:https://blog.csdn.net/epdc2/article/details/53767386

  6. golang binarySearch

    func binarySearch(nodes []*node, word Text) (int, bool) { start := end := len(nodes) - // 特例: { // 当 ...

  7. hdp spark beeline

    thriftserver端口号10016 hdp所用端口号由10000改为10016 !connect jdbc:hive2://localhost:10016

  8. 趣题: 按二进制中1的个数枚举1~2^n (位运算技巧)

    ; ; k <= n; k++){ << k)-,u = << n; s < u;){ ;i < n;i++) printf(-i)&); print ...

  9. postgresql 不同数据库不同模式下的数据迁移

    编写不容易,转载请注明出处谢谢, 数据迁移 因为之前爬虫的时候,一部分数据并没有上传到服务器,在本地.本来用的就是postgresql,也没用多久,数据迁移的时候,也遇到了很多问题,第一次使pg_du ...

  10. SQL优化工具SQLAdvisor使用(转)

    一.简介 在数据库运维过程中,优化SQL是业务团队与DBA团队的日常任务.例行SQL优化,不仅可以提升程序性能,还能够降低线上故障的概率. 目前常用的SQL优化方式包括但不限于:业务层优化.SQL逻辑 ...