Java [leetcode 4] 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 sorted arrays. The overall run time complexity should be O(log (m+n)).
解题思路:
看到时间复杂度的时候就知道这种应该使用二分查找法了,否则如果实现log的时间复杂度?
思路已经有大神提供了,说的非常清楚,附上链接地址:http://my.oschina.net/jdflyfly/blog/283267
代码如下:
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int total = nums1.length + nums2.length;
if (total % 2 == 0)
return (findKth(nums1, 0, nums1.length - 1, nums2, 0,
nums2.length - 1, total / 2) + findKth(nums1, 0,
nums1.length - 1, nums2, 0, nums2.length - 1, total / 2 + 1)) / 2;
else
return findKth(nums1, 0, nums1.length - 1, nums2, 0,
nums2.length - 1, total / 2 + 1);
}
public double findKth(int[] a, int astart, int aend, int[] b,
int bstart, int bend, int k) {
if (aend - astart > bend - bstart)
return findKth(b, bstart, bend, a, astart, aend, k);
if (astart > aend)
return b[k - 1];
if (k == 1)
return a[astart] > b[bstart] ? b[bstart] : a[astart];
else {
int la = Math.min(k / 2, aend - astart + 1);
int lb = k - la;
if (a[astart + la - 1] == b[bstart + lb - 1])
return a[astart + la - 1];
else if (a[astart + la - 1] < b[bstart + lb - 1])
return findKth(a, astart + la, aend, b, bstart, bend, k - la);
else
return findKth(a, astart, aend, b, bstart + lb, bend, k - lb);
}
}
}
Java [leetcode 4] 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 ...
- 【JAVA、C++】LeetCode 004 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 ...
随机推荐
- Vim安装ctags插件
问题描述: 系统安装ctags插件 问题解决: (1)下载ctags插件 (2)新下载的ctags文件是一个tar包文件,使用tar -zxcf命令进行解压缩 注: 解压缩之后的 ctags文件,如上 ...
- PhyreEngine3.8 MSAA resolution
There is something wrong in PhyreEngine 3.8 to fullfill MSAA, actually, I think it is eqaa You have ...
- SCI杂志更名时,如何计算影响因子?
- 数据库范式(1NF 2NF 3NF BCNF)
http://blog.csdn.net/xuxurui007/article/details/7738330 http://www.cnblogs.com/laodao1/archive/2009/ ...
- XML注入介绍--XXE,XEE,xpath等
XML注入指在请求的XML中插入攻击利用代码.根据不同的场景,可能会形成以下的漏洞形式: (1)XEE ----xml entity xpansion(xml实体膨胀,用于dos) 具体介绍:http ...
- MethodInvoker 委托
MethodInvoker 提供一个简单委托,该委托用于调用含 void 参数列表的方法. 在对控件的 Invoke 方法进行调用时或需要一个简单委托又不想自己定义时可以使用该委托. 下面的代码示例演 ...
- JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类
1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...
- java:定义线程
Thread是java.lang包的类,默认导入. 进程:操作系统中的程序,多进程即同时运行多个程序.线程:程序中的流,多线程即程序中有多个流同时执行. 一个线程用一个线程对象表示 创建线程的方法: ...
- 用于主题检测的临时日志(383b4f88-5dc7-4b08-a585-27104eb4ee7f - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(1e2a0af2-731b-4f82-9aa0-4e2d10ed7a1a - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- python mysql 简单总结(MySQLdb模块 需另外下载)
python 通过DB-API规范了它所支持的不同的数据库,使得不同的数据库可以使用统一的接口来访问和操作. 满足DB-API规范的的模块必须提供以下属性: 属性名 描述 apilevel DB-AP ...