There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.

Have you met this question in a real interview?

 
 
Example

Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5.

Given A=[1,2,3] and B=[4,5], the median is 3.

Challenge

The overall run time complexity should be O(log (m+n)).

LeetCode上的原题,请参见我之前的博客Median of Two Sorted Arrays

解法一:

class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
int n1 = A.size(), n2 = B.size();
if (n1 < n2) return findMedianSortedArrays(B, A);
if (n2 == ) return ((double)A[(n1 - ) / ] + (double)A[n1 / ]) / ;
int left = , right = n2 * ;
while (left <= right) {
int mid2 = (left + right) / ;
int mid1 = n1 + n2 - mid2;
double L1 = mid1 == ? INT_MIN : A[(mid1 - ) / ];
double L2 = mid2 == ? INT_MIN : B[(mid2 - ) / ];
double R1 = mid1 == n1 * ? INT_MAX : A[mid1 / ];
double R2 = mid2 == n2 * ? INT_MAX : B[mid2 / ];
if (L1 > R2) left = mid2 + ;
else if (L2 > R1) right = mid2 - ;
else return (max(L1, L2) + min(R1, R2)) / ;
}
return -;
}
};

解法二:

class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
int m = A.size(), n = B.size(), left = (m + n + ) / , right = (m + n + ) / ;
return (findKth(A, B, left) + findKth(A, B, right)) / 2.0;
}
double findKth(vector<int> A, vector<int> B, int k) {
int m = A.size(), n = B.size();
if (m > n) return findKth(B, A, k);
if (m == ) return B[k - ];
if (k == ) return min(A[], B[]);
int i = min(m, k / ), j = min(n, k / );
if (A[i - ] > B[j - ]) {
return findKth(A, vector<int>(B.begin() + j, B.end()), k - j);
} else {
return findKth(vector<int>(A.begin() + i, A.end()), B, k - i);
}
return -;
}
};

[LintCode] Median of Two Sorted Arrays 两个有序数组的中位数的更多相关文章

  1. [LeetCode] 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 ...

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

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

  4. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  5. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  6. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

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

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. 注解:【有连接表的】Hibernate单向N->N关联

    Person与Address关联:单向N->N,[有连接表的] #和单向1->N关联代码完全相同,控制关系的一端需要增加一个set类型的属性,被关联的持久化实例以集合形式存在. #N-&g ...

  2. IIS配置php运行环境默认加载的php.ini路径

    第一步: 把PHP的安装路径添加到环境变量Path中,右键 “我的电脑” -> 高级 -> 环境变量 -> 系统变量,追加 D:PHP-5.2.8\; 第二步: 新建“系统变量” P ...

  3. gdb调试小结

    gdb最基本的调试命令. 1以调试程序test.cpp为例: 进入调试环境 gdb test 2.b 12 在文件的第12行设置断点. 删除断点: info b 列出所有的断点信息 (gdb) inf ...

  4. Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)

    题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...

  5. Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)

    题目链接:http://codeforces.com/problemset/problem/263/D 思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访 ...

  6. Linux学习笔记(22) Linux启动管理

    1. 系统运行级别 运行级别 含义 关机 1 单用户模式,可想象为windows的安全模式,主要用于系统修复 2 不完全的命令行模式,不含NFS服务 完全的命令行模式,就是标准字符界面 4 系统保留 ...

  7. 锤子banner

    查看效果: http://js.jirengu.com/negor/4/edit?html,output <!DOCTYPE html> <html> <head> ...

  8. 【转】Spark-Sql版本升级对应的新特性汇总

    Spark-Sql版本升级对应的新特性汇总 SparkSQL的前身是Shark.由于Shark自身的不完善,2014年6月1日Reynold Xin宣布:停止对Shark的开发.SparkSQL抛弃原 ...

  9. JMeter参数化(一)

    JMeter参数化的4种方法:

  10. Liferay 6.2 改造系列之二十四:修改liferay密码的加密方式

    为了便于后期与Cas集成过程中使用数据库用户的方便,将liferay密码的加密方式改为SHA. 在/portal-master/portal-impl/src/portal.properties配置文 ...