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. qt下的跨目录多工程编译(转)

    这里要编译的工程包含一个库和一个可执行文件.可执行文件依赖于库,所以要先编译库,编译后库放在lib目录里面,可执行文件放在bin目录里面. 目录结构如下: 全局的工程文件complex.pro在工程根 ...

  2. javascript总结27 :特殊引用类型String/Number/Boolean

    为了方便操作基本数据类型,JavaScript还提供了三个特殊的引用类型:String/Number/Boolean 1  Number 例如: var s1 = "zhangsan&quo ...

  3. Params应用

    有时候我们要想传递可变数量的参数改怎么办??Params给我们提供了一个很好的方法 Parmas: 1.只运用方法的最后一位参数 2.这个参数只能标志任意类型的一位数组 3.添加了params这个参数 ...

  4. idea中dependencies中总是有红色波浪线(缺少dependency)问题

    使用IDEA进行maven开发时,将新项目import进工作空间时,Maven Projects栏中的dependencies中总是有红色波浪线,如下图: 但是这些jar在我本地的maven仓库中实际 ...

  5. Android开发环境包下载地址

    Android SDK Android NDK Android Studio 官方下载地址   (网上转来的) 如果下载速度很慢或者无法下载,有三种解决方法 1.忍耐. 2.使用P2SP下载工具,比如 ...

  6. Java之enum

    枚举是 JDK 1.5  中引入的新特性,存放在 java.lang 包中.在没有枚举之前都是直接定义一个final string这种,有了枚举之后可以直接定义啦,不过在java中需要自定义转换,自己 ...

  7. Exception has been thrown by the target of an invocation

    I'd suggest checking for an inner exception. If there isn't one, check your logs for the exception t ...

  8. Windows store app[Part 4]:深入WinRT的异步机制

    接上篇Windows store app[Part 3]:认识WinRT的异步机制 WinRT异步机制回顾: IAsyncInfo接口:WinRT下异步功能的核心,该接口提供所有异步操作的基本功能,如 ...

  9. RobotFramework与Redis库连接

    首先导入:RedisLibrary 具体写法 #连接Redis ${redis_conn} RedisLibrary.Connect To Redis ${DB_host} #获取验证码 ${smsV ...

  10. 在 Mac OSX 上安装 nginx

    今天在使用 brew 安装 nginx 时,提示错误,安装不上去: brew install nginx, 提示:/usr/local is not writable. 这个是需要修改 /usr/lo ...