【题目描述】

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的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  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 ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. 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 ...

  8. 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 ...

  9. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  10. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

随机推荐

  1. 前端隐藏Ios及安卓滚动条

    1.方法不通用 // .scroll_list::-webkit-scrollbar { display:none } .scroll_list::-webkit-scrollbar-track { ...

  2. 多线程中sleep和wait的区别,以及多线程的实现方式及原因,定时器--Timer

    1.  Java中sleep和wait的区别 ① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即 ...

  3. 20145314郑凯杰 《Java程序设计》第4周学习总结

    20145314郑凯杰 <Java程序设计>第4周学习总结 所有代码已上传: 教材学习内容总结 ①继承 设计程序中,因们需要设计多个模块,我想到了李晓东以前教我们的三个字"模块化 ...

  4. 20145240《网络对抗》PC平台逆向破解_advanced

    PC平台逆向破解_advanced shellcode注入 Shellcode实际是一段代码(也可以是填充数据),是用来发送到服务器利用特定漏洞的代码,一般可以获取权限.另外,Shellcode一般是 ...

  5. Apache-solr

    1.1. 下载 从Solr官方网站(http://lucene.apache.org/solr/ )下载Solr4.10.3,根据Solr的运行环境,Linux下需要下载lucene-4.10.3.t ...

  6. 再谈CSS动画 - 说点不知道的(一)贝塞尔曲线

    今天重新翻看<CSS 揭秘>"过渡与动画"一章,并把该章代码重新敲了一遍,代码托管在我的Github,在此总结一些心得. 动画的奥秘 在网页中添加动画的目的是让用户有更 ...

  7. ConcurrentHashMap实现线程安全的原理

    并发环境下为什么使用ConcurrentHashMap 1. HashMap在高并发的环境下,执行put操作会导致HashMap的Entry链表形成环形数据结构,从而导致Entry的next节点始终不 ...

  8. 2017 beijing icpc E - Rikka with Competition

    2017-09-22 22:01:19 writer:pprp As we know, Rikka is poor at math. Yuta is worrying about this situa ...

  9. Elasticsearch 使用技巧笔记

    1.重新分片 当出现Unassigned分片时,我们可以通过分片重分配解决这个问题 curl -X PUT http://192.168.0.37:9200/_cluster/settings \ - ...

  10. maven笔记(1)

    maven环境搭建:http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html 项目管理利器(Maven)——常用的构建命令1. mvn - ...