Q4:Median of Two Sorted Arrays
4. Median of Two Sorted Arrays
官方的链接:4. Median of Two Sorted Arrays
Description :
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)).
Example1:
nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example2:
nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
问题描述
给定长度分别为m和n的两个排序数组nums1和nums2,在时间复杂度O(log (m+n))内算出数组的中位数
思路
两个有序数组的中位数和Top K问题类似。这里从小到大直接定位第k个,简单理解为从nums1中获取第i个,而从nums2中获取第j=k-i个,其中i=0~k。当定位到nums[i-1]<=nums2[j]和nums[j-1]<=nums[i]即完成,当然还有边界问题。
把中位数也当作是top k问题,最后进行奇偶判断。详情可参考这里Share my O(log(min(m,n)) solution with explanation,代码也是借鉴的。
值得注意的是几个边界判断问题,比如i为0或者m,j为0或者n。
public class Q4_MedianOfTwoSortedArrays {
public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length;
int n = nums2.length;
// make sure that m <= n
if (m > n) {
return findMedianSortedArrays(nums2, nums1);
}
// n>=m,i = 0 ~ m,so j = (m + n + 1) / 2 -i > 0.
int i = 0, j = 0, imax = m, imin = 0, halfLen = (m + n + 1) / 2;
int maxLeft = 0, minRight = 0;
while (imin <= imax) {
i = (imin + imax) / 2;
j = halfLen - i;
if (i < m && nums2[j - 1] > nums1[i]) {
imin = i + 1;
} else if (i > 0 && nums1[i - 1] > nums2[j]) {
imax = i - 1;
} else {
if (i == 0) {
//the target is in nums2
maxLeft = nums2[j - 1];
} else if (j == 0) {
//the target is in nums1
maxLeft = nums1[i - 1];
} else {
maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
}
break;
}
}
//odd
if ((m + n) % 2 == 1) {
return (double)maxLeft;
}
//even
if (i == m) {
//nums1 is out of index m
minRight = nums2[j];
} else if (j == n) {
//nums2 is out of index n
minRight = nums1[i];
} else {
//others
minRight = Math.min(nums1[i], nums2[j]);
}
return (double) (maxLeft + minRight) / 2;
} public static void main(String[] args) {
new Q4_MedianOfTwoSortedArrays().findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 2 }); }
}
Q4:Median of Two Sorted Arrays的更多相关文章
- No.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 ...
- LeetCode2: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 sor ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- 4:Median of Two Sorted Arrays
here are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- leetcode 4:Median of Two Sorted Arrays
public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int t=nums1.Length+nums2.Length; in ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- Day5-T3
原题目 要开运动会了,神犇学校的n个班级要选班服,班服共有100种样式,编号1~100.现在每个班都挑出了一些样式待选,每个班最多有100个待选的样式.要求每个班最终选定一种样式作为班服,且该班的样式 ...
- 006.CI4框架CodeIgniter, 加载框架的helper辅助类,调用helper类中的各种函数
01. CI4框架作为一个很成熟的框架,给我们提供了很多helper辅助类,我们在代码中可以很方便的使用,如下所示,我们在Controllers中调用Cookies类的set_cookie函数 < ...
- 编译安装常用包+阿里镜像源-常用资源-系统-下载-科莱软件下载-docker仓库包-安全圈-杏雨梨云-图形界面安装-docker私有双仓库-阿里源报错处理-centos7目录大小
yum install apr-util apr-util-devel apr apr-devel pcre pcre-devel zlib zlib-devel openssl openssl-de ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
- POJ 1565:Skew Binary
Skew Binary Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10676 Accepted: 6807 Desc ...
- 《TensorFlow实战Google深度学习框架》笔记——TensorFlow入门
一.Tensorflow计算模型:计算图 计算图是Tensorflow中最基本的一个概念,Tensorflow中的所有计算都被被转化为计算图上的节点. Tensorflow是一个通过计算图的形式来描述 ...
- pwntool基础和ida常用操作
pwntools:http://www.91ri.org/14382.html ida:https://www.jianshu.com/p/d425140c6561
- 093-PHP数组比较
<?php $arra=array('hello','hi'); //定义多个数组 $arrb=array(1=>'hi',0=>'hello'); $arrc=array(1,2) ...
- gem5-gpu全系统模式
# 注意:安装好gem5-gpu后再配置全系统环境 # 下载全系统模拟需要的工具,详见http://gem5.org/Running_gem5#Full_System_.28FS.29_Mode,将L ...
- C++ for无限循环~
无限循环 如果条件永远不为假,则循环将变成无限循环.for 循环在传统意义上可用于实现无限循环.由于构成循环的三个表达式中任何一个都不是必需的,您可以将某些条件表达式留空来构成一个无限循环. #inc ...