4. Median of Two Sorted Arrays

  • Total Accepted: 104147
  • Total Submissions: 539044
  • Difficulty: Hard

  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   其实整个思路应该就是将两个有序数组进行排序的过程,然后根据总个数的奇偶决定中位数是一个还是两个的平均值。
 public class No_4 {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int total = nums1.length + nums2.length ;
int [] res ;
//判断最后的结果是单独一个数还是两个数的平均值
if(total%2 == 1){
res = new int [1] ;
}else{
res = new int [2] ;
}
int count = 0 ; //当前值是第count小的数
int i = 0 ; //nums1的下标
int j = 0 ; //nums2的下标
int k = 0 ;   //res的下标
int min ;
/*
* 将两个有序数组进行排序的过程
* 当total为偶数时,结果为总数的第 (total-1)/2+1个数和第(total-1)/2+2个数
* 当total为基数时,结果为总数的第(total-1)/2+1个数
* 所以,当count > (total-1)/2 时,开始存储,直到res存满为止
*/
while((i < nums1.length ) && (j < nums2.length) && (k < res.length)){
count++ ;
min = nums1[i] > nums2[j] ? nums2[j++] : nums1[i++] ;
if(count > (total-1)/2){
res[k++] = min ;
}
}
//考虑nums2先排除完而没有完全得到需要的结果的情况
while((i < nums1.length ) && (k < res.length)){
count++ ;
min = nums1[i++] ;
if(count > (total-1)/2){
res[k++] = min ;
}
}
//考虑nums1先排除完而没有完全得到需要的结果的情况
while((j < nums2.length ) && (k < res.length)){
count++ ;
min = nums2[j++] ;
if(count > (total-1)/2){
res[k++] = min ;
}
} //返回结果
if(total%2 == 1){
return res[0] ;
}else{
return ((res[0] + res[1])/2.0) ;
}
}
}

No.004 Median of Two Sorted Arrays的更多相关文章

  1. LeetCode--No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  2. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  3. 【JAVA、C++】LeetCode 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 two ...

  4. 【LeetCode】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 ...

  5. 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 two ...

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

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

  7. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  8. [LintCode] 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 sorted ...

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

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

随机推荐

  1. 我的Android最佳实践之—— 解决闪空界面问题

    进入应用时,由于应用的启动Activity都会有默认的theme,所以会跳一下原始界面,才启动我们定义的theme. 修改这个问题的方法,就是给应用启动的Activity设置一个空的theme.如下面 ...

  2. SSH登录很慢问题的解决

    用ssh连其他linux机器,会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析的问题 OpenSSH在用户登录的 ...

  3. OAF_架构MVC系列2 - Model的概述(概念)

    2014-06-22 Created By BaoXinjian

  4. 【JavaScript】微信适配的Head

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <me ...

  5. Tomcat的8009端口AJP的利用

    Tomcat在安装的时候会有下面的界面,我们通常部署war,用的最多的是默认的8080端口. 可是当8080端口被防火墙封闭的时候,是否还有办法利用呢? 答案是可以的,可以通过AJP的8009端口,下 ...

  6. java中通过位运算实现多个状态的判断

    通过 <<  |  & ~ 位运算,实现同时拥有多个状态 通过 << 定义数据的状态 public interface LogConstants { /** * 消耗标 ...

  7. LNMP安装了哪些软件?安装目录在哪?

    LNMP官网:http://lnmp.org/faq/lnmp-software-list.html LNMP一键安装包除去安装所必须的依赖包,还会默认安装以下软件: Nginx.MySQL/Mari ...

  8. Dephi的同一个线程支持累次Execute吗

    Suspend放到循环里------解决方案--------------------执行完不结束只休眠.另外还需要线程池.------解决方案--------------------    while ...

  9. Linux安装Oracle报Checking operating system version must be redhat-3, SuSE-9, redhat-4

    解决办法:vi /xx/database/install/oraparam.ini 找到[Certified Versions] Linux=redhat-3,SuSe-9,redhat-4,后面加上 ...

  10. Spring SpEL表达式的理解

    Spring的IOC本质就一个容器,也就是一个对象的工厂,我们通过配置文件注册我们的Bean对象,通过他进行对象的组装与床架. SpEL表达式就是一种字符串编程,类似于JS里面的EVAL的作用,通过它 ...