leetcode 之Median of Two Sorted Arrays(五)

找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值。
参考下面的思路,非常的清晰:

代码:
double findMedianofTwoSortArrays(int A[], int B[], int m, int n)
{
int total = m + n;
//判断序列长度的奇偶,奇数只有一个中间值,偶数有两个中间值,取平均
if (total & 0x1)
return find_kth(A, m, B, n, total / + );
else
return (find_kth(A, m, B, n, total / ) + find_kth(A, m, B, n, total / + )) / ;
} int find_kth(int A[], int m, int B[], int n, int k)
{
//设定m<=n
if (m > n)return find_kth(B, n, A, m, k);
if (m == )return B[k - ];
if (k == )return min(A[], B[]); //删除掉一部分数据
int ia = min(k / , m), ib = k - ia;
if (A[ia - ] < B[ib - ])
return find_kth(A + ia, m - ia, B, n, k - ia);
else if (A[ia - ]>B[ib - ])
return find_kth(A, m, B + ib, m - ib, k - ib);
else
return A[ia - ];
}
leetcode 之Median of Two Sorted Arrays(五)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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 (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 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 & 归并排序
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 ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
随机推荐
- 【BZOJ1391】Order(网络流,最小割)
[BZOJ1391]Order(网络流,最小割) 题面 BZOJ权限题... 良心洛谷 题目描述 有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成 ...
- BZOJ4152:[AMPPZ2014]The Captain——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4152 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1 ...
- spark(四)
一. spark 2 版本 相对于以前版本的变化 spark core : Accumulators (累加器):性能更好,页面上也可以看到累加器的信息 spark sql: 1. 2.DataS ...
- 【loj6198】谢特
Portal -->loj6198 Solution (为什么感觉loj上面这几道后缀数组的题..套路都是一样的啊qwq) 同样也是..考虑某个区间\(height[i]\)的最小值的贡 ...
- 图像BMP格式介绍
1 图像BMP格式说明 BMP是一种与硬件设备无关的图像文件格式,使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大.BMP文件的图像深度可选 ...
- 1.4 Matplotlib:绘图
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- 关于HttpURLConnection/HttpsURLConnection请求出现了io.filenotfoundexception:url的解决方法
//从输入流读取返回内容InputStream is = null;int status = connection.getResponseCode();if(status>= HttpStatu ...
- [DeeplearningAI笔记]序列模型3.1基本的 Seq2Seq /image to Seq
5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1基础模型 [1] Sutskever I, Vinyals O, Le Q V. Sequence to Se ...
- DLL基本知识
一.生成方式: 使用DEF文件定义导出接口或使用__declspec(dllexport)描述接口,编译链接后生成dll+lib,其中lib是导入库,里面只有对导出接口的描述,而没有具体实现. 二.链 ...
- 其他:strtok和sscanf结合输入读取一行整数
gets(buf); int v; char *p = strtok(buf," "); while(p) { sscanf(p,"%d",&v); p ...