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

Subscribe to see which companies asked this question


【题目分析】

给定两个升序排列的整数数组,找到这两个数组所有元素的中位数。


【思路】


【java代码】

 public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] A = nums1, B = nums2;
int m = nums1.length, n = nums2.length; if(m > n){
A = nums2; B = nums1;
m = nums2.length; n = nums1.length;
} if(n == 0) return 0; int mini = 0, maxi = m;
int i = 0, j = 0;
int maxofleft, minofright = 0; while(mini <= maxi){
i = (maxi - mini)/2 + mini;
j = (m + n + 1)/2 - i;
if(j > 0 && i < m && B[j-1] > A[i]){
mini = i + 1;
} //i is too small
else if(i > 0 && j < n && A[i-1] > B[j]){
maxi = i - 1;
} //i is too big
else{
if(i == 0) maxofleft = B[j-1];
else if(j == 0) maxofleft = A[i-1];
else maxofleft = Math.max(A[i-1], B[j-1]); if((m + n)%2 == 1) return maxofleft; if(i == m) minofright = B[j];
else if(j == n) minofright = A[i];
else minofright = Math.min(B[j], A[i]); return (double)(maxofleft + minofright) / 2.0;
}
}
return 0;
}
}

LeetCode OJ 4. Median of Two Sorted Arrays的更多相关文章

  1. 【LeetCode OJ】Median of Two Sorted Arrays

    题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...

  2. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

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

  3. 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. leetcode.C.4. Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...

  5. leetcode第二题--Median of Two Sorted Arrays

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

  6. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

  7. Leetcode Array 4 Median of Two Sorted Arrays

    做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...

  8. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

  9. 【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 t ...

随机推荐

  1. CoreJavaE10V1P3.4 第3章 Java的基本编程结构-3.4 变量

    1.在Java中,每一个变量都必须有一个类型,在变量声明是,类型必须在变量名之前.示例如下: double salary; int vacationDays; long earthPopulation ...

  2. 工具-maya2014软件操作细节(持续更新……)

    整体 ------------------------------------------------------- [全选控制器] 1.选中总控-显示-隔离选择-查看选定对象 2.全选并创建新层(不 ...

  3. centos-php安装

    初学者自编文档,如有错误,请指出,具体命令就不阐述了,不明白 度娘吧! nginx我是编译安装在服务器上 和其他安装应该会有区别 安装路径路径:/usr/local/ 安装包存放位置:/home/ap ...

  4. javaweb之session过期验证

    session过期判断的基本思想:用户登录成功后,将用户账号信息保存在session中,然后几乎每次执行命令都要经过过滤器,过滤器检查session中是否存在账号,若不存在, 则返回登录页面,反之正常 ...

  5. C++把引用作为返回值

    当返回一个引用时,要注意被引用的对象不能超出作用域.所以返回一个对局部变量的引用是不合法的,但是,可以返回一个对静态变量的引用. int& func() { int q; //! return ...

  6. 反射机制(reflection)动态相关机制

    功能:动态获取类的信息以及动态调用对象的方法. Java反射机制主要提供了以下功能: 1.在运行时判断任意一个对象所属的类. 2.在运行时构造任意一个类的对象. 3.在运行时判断任意一个类所具有的成员 ...

  7. Python基础(十一)-面向对象

    三种编程范式: 1.函数式编程:函数指数学意义上的函数 由于命令式编程语言也可以通过类似函数指针的方式来实现高阶函数,函数式的最主要的好处主要是不可变性带来的.没有可变的状态,函数就是引用透明(Ref ...

  8. xshell 注册码

    Xshell 5 注册码: 101210-450789-147200Xftp 5 注册码:101210-450789-147200 Xmanager 5 注册码:101210-450789-14720 ...

  9. ANSII 与Unicode,Utf8之间的转换

    在项目开发中,我们难免会遇到各种问题,特别是字符直接的转换,这里列举字符直间转换的代码: using namespace std; wstring AnsiiToUnicode(const strin ...

  10. tflearn save模型异常

    存储模型始终无法形成单个文件,最初以为是机器中间断电\休眠引起的,重复了3次之后,发现这个问题一直存在.(每一次都要跑8~9个小时啊,摔...) 解决办法:tensorflow的版本回退到0.11版 ...