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的平均值.故偶数数目的中间 ...
随机推荐
- poj 1936 All in All(水题)
题目链接:http://poj.org/problem?id=1936 思路分析:字符串子序列查找问题,设置两个指针,一个指向子序列,另一个指向待查找的序列,查找个字符串一次即可判断.算法时间复杂度O ...
- 高级爬虫工程师(Spider)-美团网-拉勾网-最专业的互联网招聘平台
高级爬虫工程师(Spider)-美团网-拉勾网-最专业的互联网招聘平台 高级爬虫工程师(Spider)
- Android短彩信源码解析-短信发送流程(二)
转载请注明出处:http://blog.csdn.net/droyon/article/details/11699935 2,短彩信发送framework逻辑 短信在SmsSingleRecipien ...
- windowsphone中获取手机位置信息
首先在界面中加入一个textblock控件以显示信息 using System; using System.Collections.Generic; using System.IO; using Sy ...
- Android面试题整理(1)
1.Activity的生命周期 onCreate(Bundle saveInstanceState):创建activity时调用. onStart():activity可见时调用 ...
- 2015 款 Macbook Pro 的 ForceTouch 触控板开启 三指拖动
RT, 默认的触控板设置中没有了三指拖动这个选项, 但是可以通过勾选 辅助功能 -> 鼠标与触控板 -> 触控板选项 中的 启用拖移 来启用三指拖动...
- php数组操作小结
$x unset($x[3]) //key不重排
- Python3.5.1 下使用HTMLParser报错
pip 安装HTMLParser之后,import HTMLParser 使用的时候,报错"ImportError:Can't not find module markupbase" ...
- 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)
Use Case: 两个程序共享同一个Configuration文件,当一个程序作出改变的时候,需要另外一个程序能够及时响应. 之前其实猜的八九不离十,估计是有一个Timer,然后定时查询Config ...
- 集合判断null
Java 引用和指针差不多,null 引用 相当于 C++的空指针. isEmpty() 用于判断List内容是否为空,即表里一个元素也没有, 但是必须在 List<MallNews> g ...