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 ...
随机推荐
- js 增加 onclick 事件
obj.onclick = function(){check(this)} function check(obj){ alert(obj)l }
- jenkins 杀死衍生进程
解决方法-1: 在execute shell输入框中加入BUILD_ID=DONTKILLME,即可防止jenkins衍生进程 解决方法-2: 修改/etc/sysconfig/jenkins配置,在 ...
- 【JS】倒计时
描述: 先要链接jquery.js,这样写法可以直接放JS文件运行. //放在图片里定位的倒计时 //顶图里面定位才使用的代码 document.writeln("<style> ...
- ES 6 : 字符串的扩展
1. 字符的Unicode表示法 JavaScript允许表示\u0000—\uFFFF之间的字符.超出这个范围,必须用2个双字节的形式表达.如:"\u20BB7"是汉字 &quo ...
- 浙大pat1013题解
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- git在webstorm中的使用
打开webstorm新建项目,这里新建的项目名称我起为lianxi 打开设置选项里的插件栏 搜索gitignore,并安装,我这里已安装,所以显示X,没有安装的会显示一个绿色的下载箭头.安装完后需要重 ...
- Bower使用教程(限window)
注意:其实用法大致相同,但是为了标准一些,这里还是标注一下window平台 Bower的使用教程非常简单,官网讲的很详细.这里再次赘述一遍. 使用bower,首先要知道bower是干什么的:bower ...
- informix数据迁移工具使用介绍
一.dbschema USAGE: dbschema [-q] [-t tabname] [-s user] [-p user] [-r rolename] [-f procname] ...
- Mac软件记录
前端: Brackets,sourceTree,dreamweaver,ps,ai,softmatic Weblayers. phpStorm,pyCharm,IDEA,eaclipse,XCODE, ...
- power oj 2480 放积木[二进制状压DP]
题目链接[https://www.oj.swust.edu.cn/problem/show/2480] 题意:中文题目. 题解:二进制状态转移+坏点判断. #include<cstdio> ...