Median of Two Sorted Arrays-----LeetCode
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)).
解题思路:
该题可以解决所有求有序数组A和B有序合并之后第k小的数!
该题的重要结论:
如果A[k/2-1]<B[k/2-1],那么A[0]~A[k/2-1]一定在第k小的数的序列当中,可以用反证法证明。
具体的分析过程可以参考http://blog.csdn.net/zxzxy1988/article/details/8587244
class Solution {
public:
double findKth(int A[], int m, int B[], int n, int k)
{
//m is equal or smaller than n
if (m > n)
return findKth(B, n, A, m, k);
if (m == )
return B[k-];
if (k <= )
return min(A[], B[]);
int pa = min(k / , m), pb = k - pa;
if (A[pa-] < B[pb-])
{
return findKth(A + pa, m - pa, B, n, k - pa);
}
else if(A[pa-] > B[pb-])
{
return findKth(A, m, B + pb, n - pb, k - pb);
} else
return A[pa-];
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int k = m + n;
if (k & 0x1)
{
return findKth(A, m, B, n, k / + );
} else
{
return (findKth(A, m, B, n, k / ) + findKth(A, m, B, n, k / + )) / ;
}
}
};
Median of Two Sorted Arrays-----LeetCode的更多相关文章
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- 3.Median of Two Sorted Arrays Leetcode
难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
随机推荐
- Spring 事务配置5种方式
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- 《C和指针》读书笔记 第2章-基本概念
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936485.html
- Linux/Unix shell 脚本监控磁盘可用空间
Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少.下面是给出的一个监控磁盘空间空间shell脚本的样本,供大家参考. 1.监控磁盘的空闲空间shell脚本 robi ...
- iOS设计模式——Category
什么是Category Category模式用于向已经存在的类添加方法从而达到扩展已有类的目的,在很多情形下Category也是比创建子类更优的选择.新添加的方法同样也会被被扩展的类的所有子类自动继承 ...
- 用VS2010打开vs2008
用VS2010打开vs2008,对应的9.0.0.0版本,而应该该使用版本10.0.0.0 解决方案:修改配置文件.将Microsoft.ReportViewer的版本为9.0.0.0改为10.0.0 ...
- c# 读取IntPtr 中的数据 z
c++的写法是这样的: LRESULT CPictureQueryDlg::OnQueryPicNty(WPARAM wp, LPARAM lp) { EnableWindow(TRUE); BYTE ...
- [整] Android Fragment 生命周期图
1. onAttach ------called once the fragment is associated with its activity 2. onCreate-------called ...
- Windows执行打开文件命令
ShellExecute(NULL, "open", localFile.c_str(), NULL, NULL, SW_SHOW); 会调用该文件类型关联的 ...
- centreon load average 的含义
下面图是centreon监控到的 load 信息 其中的 load1,load5,load15 分别说明上一分钟.最后五分钟以及最后十五分钟的系统负载均值.它们的数字当然是越小越好.数字越高,说明服务 ...
- .NET下用C#实现邮箱激活功能
最近要用到安全邮箱激活的功能,故写篇博客记录下. 思路:在表中增加一个字段State来记录邮箱是否激活(0激活,1未激活.) 1.发送邮件. 1-1,给邮箱发送邮件.内容:激活地址+GUID. ...