题目:

  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
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len1=nums1.length;
int len2=nums2.length;
int k=len1+len2; if(k%2!=0){
return findMedian(nums1,0,len1,nums2,0,len2,k/2+1);
}else{
return (findMedian(nums1,0,len1,nums2,0,len2,k/2)+findMedian(nums1,0,len1,nums2,0,len2,k/2+1))/2;
}
}
public static double findMedian(int[] nums1,int start1,int end1,int[] nums2,int start2,int end2,int k ){
if(end1>end2){
return findMedian(nums2,start2,end2,nums1,start1,end1,k);
} if(end1<=0){
return nums2[start2+k-1];
} if(k==1){
return Math.min(nums1[start1],nums2[start2]);
} int k1=Math.min(k/2,end1);
int k2=k-k1;
if(nums1[start1+k1-1]<nums2[start2+k2-1]){
return findMedian(nums1,start1+k1,end1-k1,nums2,start2,end2,k-k1);
}else{
return findMedian(nums1,start1,end1,nums2,start2+k2,end2-k2,k-k2);
}
}
}

  

【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数的更多相关文章

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

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

  3. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

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

  5. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

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

  6. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

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

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

  8. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

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

  9. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

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

随机推荐

  1. 【243】◀▶IEW-Unit08

    Unit 8 Environment I. 不定式(to do)在雅思写作中的运用 1)名词 • 主语(句首) To protect the environment is everyone's dut ...

  2. sass安装方法,绝对好用的方式

    系统重做了,很多东西都重装,sass也一样,结果在安装的过程中遇到了问题,这里记录下,也给同样遇到问题的朋友们一个思路.本方法是参照http://www.w3cplus.com/sassguide/i ...

  3. Tomcat访问程序外的上传文件

    自己在编写程序时,把图片上传到程序根目录下,但是页面使用<img> 没有显示.但是,当我刷新项目下文件夹后,页面刷新可以显示. 我通过网上查询,当在Tomcat下的server.xml配置 ...

  4. asp.net mvc cookie超时返回登录页面问题

    filterContext.HttpContext.Response.Write("<script>top.location.href = '/Login/Index';< ...

  5. 文件解析库doctotext源码分析

    doctotext中没有make install选项,make后生成可执行文件 在buile目录下面有.so动态库和头文件,需要的可以从这里面拷贝 build/doctotext就是可执行程序.   ...

  6. Android下如何计算要显示的字符串所占的宽度和高度

    Rect bounds = new Rect(); String text = "Hello World"; TextPaint paint; paint = findViewBy ...

  7. 模板 - 动态规划 - 数位dp

    #include<bits/stdc++.h> using namespace std; #define ll long long ]; ll dp[][/*可能需要的状态2*/];//不 ...

  8. 求n位水仙花数

    求n位水仙花数 A.两个关键 1.n位水仙花数的范围是什么? n位水仙花数的范围是[10n-1,10n) 2.如何判断是否为水仙花数 核心操作: 2-1.如何得到每一位? A.核心思想 对得到的数进行 ...

  9. ldap 报错整理

    1.httpd 无法启动 先用systemctl status httpd 查看一下日志 1.提示端口号是否冲突,修改httpd.conf端口号 2.提示没有权限:检查selinux,防火墙是否关闭或 ...

  10. Java流程控制和数组

    流程控制 Java中三种基本的流程控制结构:顺序结构,分支结构和循环结构. 顺序结构,任何编程语言中都会有的程序结构. 分支结构:Java语言中常见的两种, if语句和switch语句. if语句,使 ...