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. NOR Flash擦写和原理分析 (二)

    Nor Flash上电后处于数据读取状态(Reading Array Data).此状态可以进行正常的读.这和读取SDRAM/SRAM/ROM一样.(要是不一样的话,芯片上电后如何从NorFlash中 ...

  2. 一探前端开发中的JS调试技巧

    前言 调试技巧,在任何一项技术研发中都可谓是必不可少的技能.掌握各种调试技巧,必定能在工作中起到事半功倍的效果.譬如,快速定位问题.降低故障概率.帮助分析逻辑错误等等.而在互联网前端开发越来越重要的今 ...

  3. system函数

    system两层含义: 1.正确退出后.还需要再判断,操作成功或者操作失败. 2.错误退出. #include <stdio.h> #include <stdlib.h> #i ...

  4. linux下安装mysql数据库与相关操作

    如下命令都是用root身份安装,或者在命令前加上sudo 采用yum安装方式安装 yum install mysql #安装mysql客户端 yum install mysql-server #安装m ...

  5. Hbase与hive整合

    //hive与hbase整合create table lectrure.hbase_lecture10(sname string, score int) stored by 'org.apache.h ...

  6. codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp

    题意: 给出n,m,g,求好串的个数 0 <= n,m <= 10^5,n + m >= 1,0 <= g <= 1 好串的定义: 1.只由0,1组成,并且恰好有n个0, ...

  7. 两款HTTP流量分析工具HttpWatch与Fiddler的比较(转)

    最近突然想看看HTTP的消息到底是怎么回事,在网上搜了几款软件来监控,并且搜到了下面的文章,感觉介绍的不错,就copy下来了.下文摘自:http://www.imkevinyang.com/2009/ ...

  8. [JavaScript] JS中对Base64的解析

    JS中对Base64的解析 <script type="text/javascript"> /** * UTF16和UTF8转换对照表 * U+00000000 – U ...

  9. [ActionScript 3.0] AS3 访问舞台上元件的方法

    文档类: package { import flash.display.MovieClip; public class Main extends MovieClip { public function ...

  10. 卸载oracle

    1.   开始->设置->控制面板->管理工具->服务   停止所有Oracle服务.    2.   开始->程序->Oracle   -   OraHome81 ...