题目:

  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. office word标题前面的编号变成黑色方块而不显示数字编号的解决方法

    编写项目文档,文档的模板是office2003版的,文件后缀是.doc. 安照模板的格式把文档编写完成后保存.因为我是2010版的office,所以就保存成2010格式的文件后缀变成了.docx. 结 ...

  2. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

  3. C# 获取外网IP和运营商和城市

    /// <summary> /// 获取客户端外网IP,省份,城市,运营商 /// 2012年12月18日 15:07 /// </summary> public class ...

  4. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

  5. 反射(type和assembly)

    这里简要介绍type和assembly 自定义特性 为了理解编写自定义特性的方式,应了解一下在编译器遇到代码中某个应用自定义特性的元素时,该如何处理. [AttributeUsage(Attribut ...

  6. ZOJ2898【折半搜索】

    题意: 给出一系列值和对应的陷阱,对于陷阱如果存在两个就抵消,求价值最大. 思路: 折半枚举,利用异或 #include <bits/stdc++.h> using namespace s ...

  7. spring oauth2.0 实现原理

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  8. 使用tcpdump抓取EOS帐户创建与交易数据

    基本环境配置 EOS 版本:1.0.1(2018-06-04更新)   节点nodeos http监听端口:127.0.0.1:8888 本地钱包keosd http监听端口:127.0.0.1:89 ...

  9. Python-14-抽象及关键字参数

    可使用内置函数callable判断某个对象是否可调用 >>> import math >>> x = 1 >>> y = math.sqrt &g ...

  10. 解决lnmp无法远程登录的bug

    使用lnmp一键安装好linux环境后,不能远程连接到所在服务器的mysql,但是通过80端口可以登录phpmyadmin,发现原来是因为lnmp的大多数版本为了安全不禁止远程连接mysql,方法很简 ...