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. oracle学习笔记(二)设置归档模式

    设置归档模式(mount状态) ALTER database ARCHIVELOG; //关闭数据库 shutdown immediate //启动数据库到mount状态 startup mount ...

  2. Wordpress-数据库结构分析(转)

    之前用了一段时间的Wordpress,觉得真的是一个优秀的博客系统,而且有大量的主题和插件.是我目前用过的最棒的一个博客平台.不仅如此,Wordpress 的代码也是写得非常漂亮,很适合用来学习PHP ...

  3. python(21)实现多进程

    参考链接:http://www.cnblogs.com/kaituorensheng/p/4445418.html python多进程:multiprocessing python中的多线程其实并不是 ...

  4. JAVA 网格布局管理器

    //网格布局管理器 import java.awt.*; import javax.swing.*; public class Jiemian3 extends JFrame{ //定义组件 JBut ...

  5. tcpdump命令--实用篇

    //查看本机与mysql的操作命令 注意 -i any表示监听所有网络接口,我们也根据自身情况选择网络接口 #tcpdump -i any -w - dst port 3306 |strings // ...

  6. Gradle学习

    Gradle是一种构建工具,它抛弃了基于XML的构建脚本,取而代之的是采用一种基于Groovy的内部领域特定语言.近期,Gradle获得了极大的关注,这也是我决定去研究Gradle的原因. 这篇文章是 ...

  7. LInux SSH远程文件/目录传输命令scp(转载)

    From:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS是经常会在不同VPS间相互备份数据或转移数据.一般情况下VPS都已安装了Ngnix或者 ...

  8. Java中Filter、Servlet、Listener的学习

    1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个reques ...

  9. iOS 版本更新检查

    #pragma mark ---------------------------------- #pragma mark -- 检测版本更新 -(void)onCheckVersion { NSDic ...

  10. 在VMware虚拟机中配置DOS汇编开发环境!!

    操作系统:win7 32位 DOS环境:DosBox  下载:http://www.dosbox.com/ 选择当前适合自己版本,下载就可以了. 汇编编译器:MASM 5.0 下载:http://do ...