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. 使用Word2016发布CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  2. 解剖JavaScript中的null和undefined【转】

    在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...

  3. 【扫盲贴】为什么屏幕分辨率是 640x480

    本文原地址:http://www.easyx.cn/skills/View.aspx?id=172 常见的屏幕分辨率很奇怪,为什么总用一些不零不整的数字?比如以前最常见的分辨率是 640x480,当初 ...

  4. springDao的jdbctemplate

    pom文件 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http:/ ...

  5. ubuntun16.0 登陆密码忘记

    1. 开机,如下图所示(没有装虚拟机,手机拍的图片凑合这看把): 2. 此时会有一个选项:Advanced Options for Ubuntu, 选中直接回车 ,如下图: 3. 看到里面有很多选项, ...

  6. Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析

    Linq扩展最后遗留之SelectMany,Zip,SequenceEqual源码分析 一: AsParallel [并行化查询] 这个函数的功效就是将计算结果多线程化.[并行计算] =>[多核 ...

  7. You must restart adb and Eclipse.

    打开Eclipse运行android 程序发现虚拟机启动不了提示  You must restart adb and Eclipse. 如下方式适用于端口占用的情况: 1.netstat -ano|f ...

  8. win10与子系统Ubuntu 相关配置

    系统间 文件访问: 1. 在win10环境下访问Ubuntu文件系统的home目录:C:\Users\xxx\AppData\Local\Packages\CanonicalGroupLimited. ...

  9. First App on Phonegap | Cordova

    Phonegap简介 PhoneGap是一能够让你用普通的web技术编写出能够轻松调用api接口和进入应用商店的 html5应用开发平台,是唯一支持7个平台的开源移动框架. 优势: 1.兼容性:多平台 ...

  10. c# Quartz.net的简单封装

    分享一个以前封装的Quartz.net类. 新建一个QuartzClass类库项目.nuget控制台输入 image.png 添加Quartz.net的引用. 我们新建一个JobBase.cs文件,里 ...