LeetCode题解——Median of Two Sorted Arrays
题目:
找两个排序数组A[m]和B[n]的中位数,时间复杂度为O(log(m+n))。
解法:
更泛化的,可以找第k个数,然后返回k=(m+n)/2时的值。
代码:
class Solution
{
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total = m + n;
if(total & 0x1) //总数为奇数,返回中位数
return (double)findKthSortedArrays(A, m, B, n, total/ + );
else //总数为偶数,返回中间两个数的平局值
return (findKthSortedArrays(A, m, B, n, total/) +
findKthSortedArrays(A, m, B, n, total/ + )) / 2.0;
} private:
int findKthSortedArrays(int A[], int m, int B[], int n, int k)
{
if( m > n ) //保证第一个数组比第二个短
return findKthSortedArrays(B, n, A, m, k);
if( m == ) //短数组为空
return B[k - ];
if( k == ) //求第1个元素
return min(A[], B[]); int ia = min(k/, m), ib = k - ia; //比较各自的第k/2个元素,如果短数组长度小于k/2则用最后一个元素来做比较,B数组也相应调整比较的元素
if( A[ia - ] > B[ib - ] ) //短数组的大,那个短数组的右边部分必然排在第k之后,同理长数组左边的部分必然都在第k之前
return findKthSortedArrays(A, ia, B + ib, n - ib, k - ib);
else if( A[ia - ] < B[ib - ] )
return findKthSortedArrays(A + ia, m - ia, B, ib, k - ia);
else
return A[ia - ]; //如果相等则找到
}
};
LeetCode题解——Median of Two Sorted Arrays的更多相关文章
- LeetCode题解-----Median of Two Sorted Arrays
题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of t ...
- 【算法之美】求解两个有序数组的中位数 — 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(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 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 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
随机推荐
- String类中的equals()方法
在Java中,每一个对象都有一个地址空间,在这空间保存着这个对象的值. equals 比较的是值,==比较的地址以及值. 01: public class StringExample02: {03: ...
- java第五课:方法
方法交换位置的三个步骤:1.把第一个盒子里的东西拿出来,放到一边2.把第二个盒子里的东西放到第一个盒子里3.捡起刚刚放到一边的东西,放到第二个盒子里 值传递:实际参数将内部保存的值,复制给方法的参数. ...
- tomcat启动报错:Unsupported major.minor version 51.0
myeclipse中添加项目后,发现项目启动时报错:Unsupported major.minor version 51.0 因为tomcat使用的jdk版本不支持你项目的jdk版本,需要你在myec ...
- What the hell is Rotate?
- c#保留小数点后两位
double d = 23423.24234234d; Response.Write(d.ToString("0.00"));
- linux telnet命令参数及用法详解--telnet连接远程终端命令
功能说明:远端登入. 语 法:telnet [-8acdEfFKLrx][-b<主机alias.html' target='_blank'>别名>][-e<脱离字符>][ ...
- c++ ANSI、UNICODE、UTF8互转
static std::wstring MBytesToWString(const char* lpcszString); static std::string WStringToMBy ...
- 用MT.exe将exe中的manifest文件提取出来和将manifest文件放入exe中
前一种方法是将manifest文件放入exe中,但是要记得需要在工程中设置 这样的话exe中就不存在manifest了,在debug目录下就会看到相应的manifest文件.后者是将exe中的man ...
- cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案
http://blog.csdn.net/kyo7552/article/details/17163487 多种分辨率的适配一直都是一个蛋疼的问题,各家公司可能都有自己的一套方案.今天我为大家介绍的是 ...
- easyui源码翻译1.32+API翻译全篇导航 (提供下载源码)
前言 EasyUI每个组件都会有 属性.方法.事件 属性 所有的属性都定义在jQuery.fn.{plugin}.defaults里面.例如,对话框属性定义在jQuery.fn.dialog.defa ...