here 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 double findMedianSortedArrays(int[] nums1, int[] nums2) {

int len1 = nums1.length,len2 = nums2.length;

int len = len1 + len2;

int mod = len%2;

int start = 0;

int middle = len/2;

int temp = 0;

int index1 = 0,index2 = 0;

while(index1<len1 || index2 < len2){

if(index2 == len2 || (index1 < len1 && nums1[index1]<=nums2[index2])){

if(mod==1){

if(start == middle){

return nums1[index1];

}

}else{

if(start == middle - 1){

temp = nums1[index1];

}

if(start == middle){

return ((double)temp + (double)nums1[index1])/2;

}

}

index1++;

}else if(index1 == len1 || (index2 < len2 && nums1[index1]>nums2[index2])){

if(mod==1){

if(start == middle){

return nums2[index2];

}

}else{

if(start == middle - 1){

temp = nums2[index2];

}

if(start == middle){

return ((double)temp + (double)nums2[index2])/2;

}

}

index2++;

}

start ++;

}

return 0.0;

}

4:Median of Two Sorted Arrays的更多相关文章

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

  2. LeetCode2: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 sor ...

  3. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  4. Q4:Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 官方的链接:4. Median of Two Sorted Arrays Description : There are two sort ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

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

  6. 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion

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

  7. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

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

  8. leetcode 4:Median of Two Sorted Arrays

    public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...

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

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

随机推荐

  1. OBD Problem Vehicles

    This page contains a list of vehicles that are known to be non-compliant with OBD-II in one way or a ...

  2. ceph之image(转)

    原文地址:http://www.cnblogs.com/sammyliu/p/4843812.html?utm_source=tuicool&utm_medium=referral 2 卷(i ...

  3. java解决跨域

    方法中response.setHeader("Access-Control-Allow-Origin", "https://ding.taozugong.com" ...

  4. C#调用windows命令行(CMD)

    using System.Diagnostics; public static void StartCmd(String command){Process p = new Process();p.St ...

  5. 团体程序设计天梯赛L3-010 是否完全二叉搜索树 2017-03-24 16:12 29人阅读 评论(0) 收藏

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  6. 关于Java中的几种特殊类与接口,及特殊的创建实例的方法

    Java中有一些特殊的类,在教材中讲解的不深,但是确实非常有用的,这里总结一下,里面用到的有网上搜到的内容,这里表示下感谢. 一.成员内部类 成员内部类是在一个内中定义的另外一个类,这个类属于其上的类 ...

  7. maven-multiModule

    主pom的定义 packaging:pom modules的指定 dependencyManagement的指定 properties的指定 build或profile的设置 子module的创建 在 ...

  8. SQL Server 2008R2 附件数据库问题记录

    在Sql Server 2008 R2里附加数据库时弹出xxx.mdf拒绝访问的错误 详细错误信息如下: TITLE: Microsoft SQL Server Management Studio-- ...

  9. .CHM文件使用问题

    1.查看时内容为空---解除锁定 解决方案:选中xx.CHM,右键点击属性,常规选项卡中点击“解除锁定”.OK了 2.内容显示乱码---修改浏览器编码 IE中的查看 > 编码 > 自动选择 ...

  10. linux命令之进程管理命令

    1.ps:查看进程 该命令用于列出命令执行时刻的进程快照,如果想要动态的显示进程信息,可以使用top命令. 参数 说明 a(常用) 显示与终端相关的所有进程,包含每个进程的完整路径 x(常用) 显示与 ...