题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为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. 假设nums1.length = m, nums2.length = n; m < n;
2. 若(m + n) % 2 == 0, 表示两数组之和为偶数,应该是有两个中位数,因此最终结果为第9行的代码所示。否则,结果为第7行的代码所示。
3. 为了使得方法的统一,在最初时,对数组进行处理,统一使得传进方法的短数组为nums1,即第14行代码所示。
4. 如果len1-start1 == 0,则表示nums1已经全部加入前k个了,则第k个为nums2[k -1]; 在方法findKth()中的k是一直变化的,初始时,k为两个数组中排序之后的第k个数的位置;k在方法中的真正含义为“还需要找到多少个数才能达到k个”;因此假设nums1.length ==0;,此时len1-start1 == 0, 则中位数就是nums2[k - 1],即在nums1中找到了0个数,还需要找k个数,第k个数就是nums[k - 1];
5. 如果k == 1,则表示前k-1小的数已经找过了,则第k个数肯定是nums1[start1]和nums2[start2]中较小的那个数。
6. 下面接着就是常规的情况:即nums1中包含一部分k,nums2中也包含一部分的k,因此就从每个数组的k/2那里开始比较(也相当于每次都会有一半的数被加入前k个,因此时间复杂度为O(log(m + n))):
采用p1和p2分别记录当前nums1和nums2需要比较的那个位,由于nums1比较短,因此有可能2/k的位置已经超出了nums1的长度,因此nums1还需要做特殊处理,即第19行代码所示;由于p1做了特殊处理,那p2也就要做特殊处理。总之,start1~p1和start2~p2的和一定为k。
1)若nums1[p1 - 1] < nums[p2 - 1],则表明【start1, p1)之间的值在前k个数中;
2)若nums[p1 - 1] > nums2[p2- 1],则表明【start2, p2)之间的值在前k个数中;
3)若两值相等,则表明【start1, p1)+【start2, p2)的个数为k,则结果直接返回其中一个即可。
为什么比较的p1和p2的前一个位的数,而不是p1和p2位置的数呢? 举例说明:假设start1== start2 == 0, 则p1 = Math.min(len1, k / 2); p2 = k - p1,即p1 + p2 == k;;假设p1 = 5, p2 = 7;, 则k = 12; 在数组中nums[5]其实是第6个数,nums[7]其实是第8个数,所以我们比较的是nums1[p1 - 1]与nums2[p2 - 1]的值; 代码如下:
 public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1 = nums1.length;
int len2 = nums2.length;
int size = len1 + len2;
if(size % 2 == 1)
return findKth(nums1, 0, len1, nums2, 0, len2, size / 2 + 1);
else
return (findKth(nums1, 0, len1, nums2, 0, len2, size / 2) + findKth(nums1, 0, len1, nums2, 0, len2, size / 2 + 1)) /2;
}
public double findKth(int[] nums1, int start1, int len1, int[] nums2, int start2, int len2, int k)
{
if(len1 - start1 > len2 -start2) // 传进来的时候统一让短的数组为nums1
return findKth(nums2, start2, len2, nums1, start1, len1, k);
if(len1 - start1 == 0) // 表示nums1已经全部加入前K个了,第k个为nums2[k - 1];
return nums2[k - 1];
if(k == 1)
return Math.min(nums1[start1], nums2[start2]); // k==1表示已经找到第k-1小的数,下一个数为两个数组start开始的最小值
int p1 = start1 + Math.min(len1 - start1, k / 2); // p1和p2记录当前需要比较的那个位
int p2 = start2 + k - p1 + start1;
if(nums1[p1 - 1] < nums2[p2 - 1])
return findKth(nums1, p1, len1, nums2, start2, len2, k - p1 + start1);
else if(nums1[p1 - 1] > nums2[p2 -1])
return findKth(nums1, start1, len1, nums2, p2, len2, k - p2 + start2);
else
return nums1[p1 - 1]; }
}

Leetcode4--->求两个排序数组的中位数的更多相关文章

  1. leetcode4:两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 1.我的思路:直接用sort,时间复杂度应如 ...

  2. LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  3. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  4. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  5. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

  6. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  7. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

  8. 求两个排序数组中位数 C++

    题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nu ...

  9. [Swift]LeetCode4. 两个排序数组的中位数 | 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 ...

随机推荐

  1. webm视频转换 其他视频格式转换为webm格式

    将其他视频格式转换为webm格式 https://files.cnblogs.com/files/bubuchu/html5videoshipingeshizhuanhuanqi.zip

  2. Android 4.4及以后将内容布局延伸到状态栏

    首先说明:该文章不是大家说的沉浸式状态栏,网上沉浸式状态栏的博客很多,搜索就有了! 该篇博客的主要目的就是为了将图片显示在状态栏上,让APP看起来更有型!如下图所示:   界面 这个界面的布局就是co ...

  3. Object-C反射读取实体属性和值

    举例: 首先定义TestModel如下: @interface TestModel : NSObject @property (nonatomic, strong) NSString *name; @ ...

  4. Linux Centos7.2 编译安装PHP7.0.2

    操作环境: 1.系统:Centos7.2 2.服务:Nginx 1.下载PHP7.0.2的安装包解压,编译,安装: $ cd /usr/src/ $ wget http://cn2.php.net/d ...

  5. Yii2.0 两次奇葩的数据库连接经历

    经历一: 公司的项目经过阿里云的ECS升级后,发现在Yii2.0框架中,凡是数据库新增的字段(当然相关的表模型肯定是加了相应字段的),老是报“属性找不到”的问题,最后排查是数据库连接的问题.把127. ...

  6. Linux文件压缩命令

    一.zip命令(常用) 用zip命令压缩的文件在Windows系统下也是可以解压缩的,即此格式压缩文件两个系统通用. 文件压缩:zip  filename.zip   filename 目录压缩:zi ...

  7. JavaScript_3_输出

    1. JavaScript通常用于操作HTML元素,可以使用getElementById(id)方法. JavaScript由Web浏览器来执行. 2. document.write()仅仅向文档输出 ...

  8. Spring Mybatis PageHelper 设置使用

    PageHelper是一个Mybatis的分页插件, 负责将已经写好的sql语句, 进行分页加工. 设置 现在使用的是PageHelper 5.0 版本 : 在build.gradle先引用jar包: ...

  9. Oracle Real Application Clusters (RAC)

    Oracle Real Application Clusters — 概述 包含 Oracle Real Application Clusters (RAC) 选件的 Oracle 数据库允许依托一组 ...

  10. php常见验证

    /** * 文件上传 * @param $file 要上传的文件 * @param $size 大小设置 * @param $ext 文件类型 * @return bool 是否上传成功 */func ...