LeetCode OJ 4. 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 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的更多相关文章
- 【LeetCode OJ】Median of Two Sorted Arrays
题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 ...
- 【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 ...
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode.C.4. Median of Two Sorted Arrays
4. Median of Two Sorted Arrays 这应该是最简单最慢的方法了,因为本身为有序,所以比较后排序再得到中位数. double findMedianSortedArrays(in ...
- 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 ...
- 【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...
- Leetcode Array 4 Median of Two Sorted Arrays
做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法 ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- 【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 ...
随机推荐
- fpga串口通信
---恢复内容开始--- 1.波特率的计算公式:9600bps 是指每秒可以传输9600位 则一位需要的时间为1/9600 约等于0.000104 开发板晶振大小为50M则传输一位需要的时间为 0.0 ...
- 关于高性能javascript 笔记
最近买了本新书,准备自己吃 狗粮的同时也吃点精神食粮.笔记总结,从现在开始,看我啥时候能看完这本酥,就酱紫, begin:
- 两种画线算法(DDA&Bersenham)
DDA(digital differential analyzer) 由直线的斜截式方程引入 对于正斜率的线段,如果斜率<=1,则以单位x间隔(δx=1)取样,并逐个计算每一个y值 Yk+1 = ...
- 利用PyCharm进行Python远程调试
背景描述 有时候Python应用的代码在本地开发环境运行十分正常,但是放到线上以后却出现了莫名其妙的异常,经过再三排查以后还是找不到问题原因,于是就在想,要是可以在服务器环境中进行单步跟踪调试就好了. ...
- [Q]将图纸转换为JPG、PNG、plt、DWF、DWFx,XPS等格式文件
如要将图纸打印为图片,请选择“PublishToWeb JPG.pc3”或“PublishToWeb PNG.pc3”打印机. 如要将图纸打印为plt格式文件,请选择“Windows Default ...
- SAP HANA创建层次结构的属性视图(Hierarchy Attribute View)
在产品表中,数据是具有一定层次结构的 1. 在Content相应的package下面右键点击Attribute View,新建Attribute View 填写相关信息,注意:我们所建的依然是标准视图 ...
- 使用Image.GetThumbnailImage 方法返回缩略图
如果 Image 包含一个嵌入式缩略图像,则此方法会检索嵌入式缩略图,并将其缩放为所需大小. 如果 Image 不包含嵌入式缩略图像,此方法会通过缩放主图像创建一个缩略图像. 请求的缩略图像大小为 1 ...
- SQL LIKE语句多条件贪婪匹配算法
在CMS开发中,经常会有类似这样的需求: 提问——回答模式,最经典的例子就是百度提问. 提问者提出问题,由其他人回答,其他人可以是用户,也可以是服务商. 在这个模式中,如何充分利用历史数据是最关键的技 ...
- SQLServer性能优化
http://www.cnblogs.com/studyzy/archive/2008/11/24/1339772.html
- Namespacing in PHP (php 中使用命名空间)
最近学习一下php的命名空间,中文文档不多,搜到一篇英文的,讲的还蛮系统的,特此翻译一下,以备以后查阅,大家有什么高见或更深刻或者 更悟透的见解希望能不吝赐教,晚辈感激不尽. 原文: http://c ...