Leetcode: Median of Two Sorted Arrays. java.
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)).
public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int k = A.length + B.length;
return k % 2 == 0 ? (findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1) +
findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2)) / 2
: findK(A, 0, A.length - 1, B, 0, B.length - 1, k/2 + 1);
}
//返回两个数组中第k大的元素。
public double findK(int a[], int s1, int e1, int b[], int s2, int e2, int k) {
int m = e1 - s1 + 1;
int n = e2 - s2 + 1;
if (m > n) return findK(b, s2, e2, a, s1, e1, k); //a的长度比b的小。
if (s1 > e1) return b[s2 + k - 1];
if (s2 > e2) return a[s1 + k - 1];
if (k == 1) return Math.min(a[s1], b[s2]);
int midA = Math.min(k/2, m), midB = k - midA;
//假设a的第midA大的元素比b的第midB大的元素小,
//那么删掉a的前midA个元素,在剩余的数中找第k-midA大的。
if (a[s1 + midA - 1] < b[s2 + midB - 1])
return findK(a, s1 + midA, e1, b, s2, e2, k - midA);
else if (a[s1 + midA - 1] > b[s2 + midB - 1])
return findK(a, s1, e1, b, s2 + midB, e2, k - midB);
else
return a[s1 + midA - 1];
}
}
Leetcode: Median of Two Sorted Arrays. java.的更多相关文章
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- Leetcode Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- LeetCode——Median of Two Sorted Arrays
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- Median of Two Sorted Arrays(Java)
求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.u ...
- leetcode:Median of Two Sorted Arrays分析和实现
这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...
随机推荐
- QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)
原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...
- 「操作系统」:The most useful condition codes
CF: Carry Flag.The most recent operation generated a carry out of the most significant bit. Used to ...
- fedora21 codeblocks在编辑装态下无法输入
来自:http://forum.ubuntu.com.cn/viewtopic.php?f=88&t=284409 用codeblocks,突然发现怎么敲键盘都不能输入 搜索后得知: Co ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...
- Swift - 2 (?和!、结构体、类、协议、扩展、闭包)
1> 可选类型(?)和强制解包(!) 在swift中,可选类型(?) 其根源是一个 枚举型,里面有 None 和 Some 两种类型.其实所谓的 nil 就是 Optional.None , 非 ...
- Cocos2d-x 创建自己定义项目模板
你是否以前为cocos方便高速开发而兴奋,你是否以前为各种工具的便利开发而感动,但如今的你是否为每次创建一个新的项目都是HelloWorldScene而苦恼? 好吧,事实上我也感觉到了,每次创建一个项 ...
- Qt程式异常崩溃处理技巧(Win)
这篇文章谈的是 Qt4 程式在视窗系统下的异常崩溃处理技巧.所以须要在头文件里包括"#include <Windows.h>". 首先,程式难免会有异常崩溃的时候.重要 ...
- 驱动: oops
linux驱动调试--段错误之oops信息分析 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29401328&id= ...
- BZOJ 2599: [IOI2011]Race( 点分治 )
数据范围是N:20w, K100w. 点分治, 我们只需考虑经过当前树根的方案. K最大只有100w, 直接开个数组CNT[x]表示与当前树根距离为x的最少边数, 然后就可以对根的子树依次dfs并更新 ...
- 统计图表类库--libchart使用简介
1.饼图 #载入类文件 include "../libchart/classes/libchart.php"; header("Content-type: image/p ...