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 sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
【解题思路】
1、整体排序,去中间值,但是,实际上,我们不需要完整的排序完成,只需要排序middle左边的即可,这样就能保证middle所在的位置是中间位置。
2、需要考虑num1和mun2全部长度为偶数和奇数的问题,因为如果是奇数,中间值为一个,如果是偶数,中间值为两个。这个其实,终归是要在两个数组中查找一个具体的值。
3、当一个数组已经遍历完了,但没有找到值时,要在另外一个数组中继续遍历。
【代码实现】
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int total = n1 + n2;
if(total % 2 == 1)//奇数情况
{
return findMiddle(nums1, nums2, n1, n2, (total/2 + 1));
}
else
{
return (findMiddle(nums1, nums2, n1, n2, (total/2)) + findMiddle(nums1, nums2, n1, n2, (total/2 + 1)))/2;
}
}
private double findMiddle(int[] nums1, int[] nums2, int n1, int n2, int midIndex)
{
double middle = 0.0;
int i = 0, j = 0;
for(; i<n1 && j<n2; )
{
if(nums1[i] < nums2[j])
{
i++;
middle = nums1[i-1];
}
else
{
j++;
middle = nums2[j-1];
}
if((i+j) == midIndex)
{
break;
}
}
while(i<n1 && ((i+j) < midIndex))
{
i++;
middle = nums1[i-1];
if((i+j) == midIndex)
{
break;
}
}
while(j<n2 && ((i+j) < midIndex))
{
j++;
middle = nums2[j-1];
if((i+j) == midIndex)
{
break;
}
}
return middle;
}
【后续】
发现了更好的方法,通过分治来实现,方法确实巧妙。
可参考链接:
https://hk029.gitbooks.io/leetbook/%E5%88%86%E6%B2%BB/004.%20Median%20of%20Two%20Sorted%20Arrays[H]/004.%20Median%20of%20Two%20Sorted%20Arrays[H].html
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 ...
随机推荐
- Python面试题之Python迭代器
要理解迭代器,首先要从字面意思来说. 迭代 重复 下一次重复基于上一次的结果 软件开发就是典型的迭代更新. 讲迭代,我们就先来模拟一下迭代: 现在让我们使用while循环来遍历出一个列表list1 = ...
- 20145216 史婧瑶《Java程序设计》第6周学习总结
20145216 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 如果要将数据从来源中取出,可以使用输 ...
- http://www.kindsoft.net/docs/qna.html
http://www.kindsoft.net/docs/qna.html 感觉 Kindediter 非常好用 界面效果好 API也全面 很不错的编辑器
- Jquery 复制功能
使用clipboardjs插件实现鼠标点击复制功能: 官网:https://clipboardjs.com/ 使用示例: 1.引入 <script type="text/javascr ...
- CentOS 7 SSH远程证书登陆
SSH远程证书登陆是使用"公私钥"认证的方式来进行SSH登录. 1.创建公私钥 创建方式有很多种,比如说通用ssh连接工具创建,然后把公钥上传到Server主机对应的用户目录下: ...
- html-常用块级及行级标签
1.常见块级标签 <h1></h1>......<h6></h6>:标题标签 h标签:标题标签,自动加粗,h1最大,h6最小 例:(前后隔一行) ...
- POJ 1208 模拟
2017-08-28 15:07:16 writer:pprp 好开心,这道题本来在集训的时候做了很长很长时间,但是还是没有做出来,但是这次的话,只花了两个小时就做出来了 好开心,这次采用的是仔细分析 ...
- Math.Round 四舍五入问题 解惑 !
前言: 一定要说 坑爹的微软! 坑爹的微软! 坑爹的微软! 重要的事情说 三遍 ! 摘录 SMDN 示例 下面的示例演示就近舍入. C# C++ VB Math.Round(3.44, 1); ...
- 04_Storm编程上手_WordCount集群模式运行
1. 要解决的问题:代码打包 前一篇的代码,在IDEA中通过maven工程创建,通过IDEA完成代码打包 1)File -> Project Structure 2) 选择Artifacts, ...
- 分页式存储管理方式AND请求分页式存储管理
先说下什么是页(页面):就是将用户的程序的的地址空间分成固定大小的区域,称为”页“,或者”页面“ 之后将这些页离散的放进内存中,这样解决了内存的碎片问题 记得老师上课说了下这两个概念不能混,现在区分下 ...