No.004 Median of Two Sorted Arrays
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的更多相关文章
- LeetCode--No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- 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 ...
- 【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 ...
- 【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 ...
- 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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [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 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- 138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 访问修饰符internal
internal(C# 参考) internal 关键字是类型和类型的成员 访问修饰符. 只有在同一程序集的文件中,内部类型或成员才是可访问的,如下例所示: public class BaseClas ...
- 理解MVC模式
理解一般意义上的MVC模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为以下三个基本部分: 模型(Model):模型用于封装与应用程序的业务逻 ...
- Qt属性表控件的使用 QtTreePropertyBrowser
属性表是vs2003时引入的的新控件,用于流量和设置大量的信息,现在,很多软件上都能看到它的身影,如vs,Qt Creator等IDE的详细设置里都离不开属性表. 下图是Qt Creator里的属性表 ...
- solr环境搭建
介绍摘自百度百科:Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过 ...
- CF 500D New Year Santa Network tree 期望 好题
New Year is coming in Tree World! In this world, as the name implies, there are n cities connected b ...
- NeHe OpenGL教程 第三十五课:播放AVI
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- JAVA算数运算符
算数运算符 序号 算数运算符 含义用法 特殊含义用法 1 + 加法 字符串连接 2 - 减法 3 * 乘法 4 / 除法 5 % 取余 实例: public class Test{ p ...
- iostat命令详解 IO性能分析
简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...
- iOS 加入自定义字体方法
1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序添加以下代码 输出所有字体 NSArray *familyNames = [UIFont fa ...