题目:

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

  即找两个有序数组的中位数,一开始想的是用第三个数组来把前面两个数组的值依次放进去,再直接找中间的数,但是对时间复杂度的要求是O(log (m+n)),所以不能用数组存。网上找的方法是比较两个数组中间的元素,如AB两个数组,如果A[mid]>B[mid],那中位数肯定就不在B的前半段,于是缩小了范围,即B后半段加上A,然后依次查找并缩小范围,直到找到中间的数为止。

public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m=nums1.length;
int n=nums2.length;
if((m+n)%2==0)
return (findKMax((m+n)/2,nums1,0,nums2,0)+findKMax((m+n)/2+1,nums1,0,nums2,0))/2.0;//如果是偶数
else
return findKMax((m+n)/2+1,nums1,0,nums2,0);//如果是奇数
}
public int findKMax(int k,int[] nums1,int start1,int[] nums2,int start2){
if(start1>=nums1.length)//第一个数组长度为0的话,直接返回第二个数组的中位数
return nums2[start2+k-1]; if(start2>=nums2.length)//第二个数组长度为0的话,直接返回第一个数组的中位数
return nums1[start1+k-1]; if(k==1)//k=1,即找第一个,也就是nums1或nums2中最小的
return Math.min(nums1[start1], nums2[start2]); int temp1=start1+k/2-1;
int temp2=start2+k/2-1; int mid1=temp1<nums1.length?nums1[temp1]:Integer.MAX_VALUE;//nums1没有下标为temp1的元素,如果有,就用那个元素比较,没有就用最大int
int mid2=temp2<nums2.length?nums2[temp2]:Integer.MAX_VALUE; if(mid1>=mid2)
return findKMax(k-k/2,nums1,start1,nums2,temp2+1);//如果mid1大,也就是nums1中间的数大,那么nums1前半段不会有中位数
else //从nums1后半段和nums1开始找
return findKMax(k-k/2,nums1,temp1+1,nums2,start2); } }

Leetcode 5——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. 关于vue-axios的url地址统一设置

    var instance = axios.create({ baseURL: 'http://qqk.com/Wechat/', headers: { 'Content-Type': 'applica ...

  2. ASP.NET Core 新核心对象WebHost(一)

    以本系列文章向Fish 前辈的那篇我心中的ASP.NET 核心对象致敬.(虽然不知道前辈现在在干什么).一晃就6年过去了,那首 郝云 的<回到那一天>怎么唱来着? 时光一晃,你就三十了. ...

  3. 启动就加载(三)initializingbean实现afterPropertiesSet方法

    TransactionTemplate,就直接以TransactionTemplate为入口开始学习. TransactionTemplate的源码如下: public class Transacti ...

  4. HttpServletRequest对象

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  5. Python Cookbook(第3版)中文版:15.21 诊断分段错误

    15.21 诊断分段错误¶ 问题¶ 解释器因为某个分段错误.总线错误.访问越界或其他致命错误而突然间奔溃. 你想获得Python堆栈信息,从而找出在发生错误的时候你的程序运行点. 解决方案¶ faul ...

  6. Xampp apache与mySQL开不了 解决办法

    Xampp安装后,打开Xampp control panel. 点击Apache对应的Start,开不了.原因是系统的服务占用了80端口,所以要么结束系统服务,要么修改apache端口. 个人比较喜欢 ...

  7. I/HwPointEventFilter: do not support AFT because of no config

    I/HwPointEventFilter: do not support AFT because of no config 这是华为对系统做了修改,默认不打印日志,要改配置 在拨号界面输入:以下进入工 ...

  8. 【洛谷T7152】(考试题目)细胞

    题面 题目描述 小 X 在上完生物课后对细胞的分裂产生了浓厚的兴趣.于是他决定做实验并 观察细胞分裂的规律. 他选取了一种特别的细胞,每天每个该细胞可以分裂出 x − 1 个新的细胞. 小 X 决定第 ...

  9. php文件上传原理详解(含源码)

    1.文件上传原理 将客户端的文件上传到服务器,再将服务器的临时文件上传到指定目录 2.客户端配置 提交表单 表单的发送方式为post 添加enctype="multipart/form-da ...

  10. Hadoop的Archive归档命令使用指南

    hadoop不适合小文件的存储,小文件本省就占用了很多的metadata,就会造成namenode越来越大.Hadoop Archives的出现视为了缓解大量小文件消耗namenode内存的问题. 采 ...