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. phonegap 随笔

    开发者论坛 http://bbs.phonegapcn.com/forum.php phone调用android本地方法 http://blog.csdn.net/crazyman2010/artic ...

  2. 接口测试:如何定位BUG的产生原因

    转自公众号<QA之道> 我们从在日常功能测试过程中对UI的每一次操作说白了就是对一个或者多个接口的一次调用,接口的返回的内容(移动端一般为json)经过前端代码的处理最终展示在页面上.ht ...

  3. Angular-ui-router + oclazyload + requirejs实现资源随route懒加载

    刚开始用angularjs做项目的时候,我用的是ng-router,觉得加载并不好.所以就用了ui-router,考虑到在app上网页加载速度太慢,所以我就想到了用懒加载,看下是否能提升性能,提高加载 ...

  4. NMON监控工具

    工具可将服务器的系统资源耗用情况收集起来并输出一个特定的文件,并可利用 excel 分析工具nmonanalyser进行数据的统计分析.并且,nmon运行不会占用过多的系统资源,通常情况下CPU利用率 ...

  5. 持续集成Jenkins+sonarqube部署教程

    1 引言 1.1 文档概要 本文主要介绍jenkins,sonar的安装与集成,基于ant,maven构建.用一个例子介绍jenkins的编译打包部署,代码检查.最后集成jenkins.(现阶段只是简 ...

  6. Python学习笔记——进阶篇【第九周】———协程

    协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...

  7. [ mysql优化一 ] explain解释select语句

    NOSQL  没有什么数据表, 只是一些变量,key_value  ,redis 支持的变量比较多.可以持久化文件到硬盘上. mysql 关系型数据库 ,表和表中间有各种id的关系. 缺点  高并发读 ...

  8. ansible 判断和循环

    标准循环 模式一 - name: add several users user: name={{ item }} state=present groups=wheel with_items: - te ...

  9. java运行时数据区域

    数据区域有:程序计步器,虚拟机栈,本地方法栈,java堆,方法区 程序计步器: 它是一块较小的内存空间,它的作用可以看做是当先线程所执行的字节码的信号指示器. 每一条JVM线程都有自己的PC寄存器,各 ...

  10. Debian搭建PPTPD

    先安装pptpd: apt-get install pptpd 编辑 /etc/pptpd.conf #取消下面两行的注释,在文件的底部. # localip 192.168.0.1 # remote ...