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 ...
随机推荐
- Matlab中bsxfun和unique函数解析
一.问题来源 来自于一份LSH代码,记录下来. 二.函数解析 2.1 bsxfun bsxfun是一个matlab自版本R2007a来就提供的一个函数,作用是”applies an element-b ...
- select框的text与value值的获取(实用版)
function def(){ var key = document.getElementById ('selectarea'); //select list var value = docum ...
- ajax返回正个页面
- [java线段树]2015上海邀请赛 D Doom
题意:n个数 m个询问 每个询问[l, r]的和, 再把[l, r]之间所有的数变为平方(模为9223372034707292160LL) 很明显的线段树 看到这个模(LLONG_MAX为922337 ...
- HandlerThread 类的学习(转载)
HandlerThread继承于Thread,所以它本质就是个Thread.HandlerThread类用于方便的创建一个含有looper的线程类,looper用来创建handler类.我们一般不创建 ...
- nginx配置负载均衡与反向代理
#给文件夹授权 1 chown -R www:www /usr/local/nginx #修改配置文件vim nginx.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- POJ2965——The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...
- Linux 下shell显示-bash-4.1$不显示用户名路径的解决方法
Linux CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法 问题描述: CentOS下新增一个用户,登录进去之后shell脚本的信息如下: 而不是我们经常看 ...
- autocapticalize和autocorrect
首字母自动大写autocapitalize 在 iOS 中,用户可以手动开启「首字母自动大写」功能,这样输入英文的时候,首字母便会自动大写.但是,有些时候并不希望一直是首字母大写的.比如用户名这个字段 ...
- linux目录/术语/英文
1./bin :获得最小的系统可操作性所需要的命令2./boot :内核和加载内核所需的文件3./dev :终端.磁盘.调制解调器等的设备项4./etc :关键的启动文件和配置文件5./home :用 ...