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 ...
随机推荐
- 【BZOJ1951】古代猪文(CRT,卢卡斯定理)
[BZOJ1951]古代猪文(CRT,卢卡斯定理) 题面 BZOJ 洛谷 题解 要求什么很显然吧... \[Ans=G^{\sum_{k|N}{C_N^k}}\] 给定的模数是一个质数,要求解的东西相 ...
- 【BZOJ4011】【HNOI2015】落忆枫音(动态规划)
[BZOJ4011][HNOI2015]落忆枫音(动态规划) 题面 BZOJ 洛谷 Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜 ...
- MSF下ms17_010_psexec模块使用技巧
0x01 前言 MS17-010 的psexec是针对Microsoft Windows的两款最受欢迎的漏洞进行攻击. CVE-2017-0146(EternalChampion / EternalS ...
- Python精要参考(第二版)
ython 精要参考(第二版) 是Python语言初学者不错的参考学习用书,本系列译自Python Essential Reference, Second Edition 希望本系列可以给python ...
- 服务器上的 Git - 在服务器上搭建 Git
http://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C%8D%E ...
- bzoj4774 修路
4774: 修路 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 290 Solved: 137[Submit][Status][Discuss] D ...
- TCP粘包处理
TCP(transport control protocol,传输控制协议)是面向连接的,面向流的,提供高可靠性服务.收发两端(客户端和服务器端)都要有一一成对的socket, 因此,发送端为了将多个 ...
- 8.Android_UiAutomator 报告查看
一.Android UiAutomator报告查看 1.错误类型 1)断言错误:就是断言这个用例的成功或者失败(AssrtionFailedError) 2)脚本错误:UiObjectNotFound ...
- Java基础之equals() 和 hashCode()
equals()是Object中的一个方法: public boolean equals(Object obj) { return (this == obj); } 在Object中equals()方 ...
- 使用HttpClient4来构建Spring RestTemplate
Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...