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.排序法 时间复 ...
随机推荐
- SQL语句利用日志写shell拿权限
outfile被禁止,或者写入文件被拦截: 在数据库中操作如下:(必须是root权限) show variables like '%general%'; #查看配置 set global genera ...
- 使用python将请求的requests headers参数格式化方法
import json # 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量 headers = """ Host: zhan.qq.com ...
- python假设一段楼梯共 n(n>1)个台阶,小朋友一步最多能上 3 个台阶,那么小朋友上这段楼 梯一共有多少种方法
我们先把前四节种数算出来(自己想是哪几类,如果你不会算,那就放弃写代码吧,干一些在街上卖肉夹馍的小生意,也挣得不少) 标号 1 2 3 4 种类 1 2 4 7 ...
- E. MaratonIME does (not do) PAs
E. MaratonIME does (not do) PAs time limit per test 2.0 s memory limit per test 256 MB input standar ...
- Linux在实际中的应用
各位童鞋们,你们是如何度过这周周末的呢?这周末的我在家学习学习再学习,然而学习到一半,公司领导突然给我打了个电话过来说有同事等会儿会去客户那部署无人值守安装系统服务,问我去不去学习下.我想我正在学Li ...
- mysql 免安装版密码操作
登录MySQL:mysql -u root -p 1.修改用户密码: mysql> GRANT ALL ON . TO ‘root’@’localhost’ IDENTIFIED BY ‘123 ...
- tornado5.0+async+await
不使用数据库的情况下实现异步 使用gen.sleep()模拟阻塞 使用gen.sleep(time) 而不是time.sleep(),time.sleep()阻塞整个进程,看gen.sleep()源码 ...
- 五十九、SAP中常用预定义系统变量
一.SAP中常用预定义系统变量 内容如下: 二.系统变量定义在结构SYST里,我们打开SE38 三.在代码编辑器输入SYST变量 四.双击SYST,来到这个系统结构,里面有很多系统变量 五.我们随便写 ...
- 五十六、SAP中LVC表格的常用布局属性LVC_S_LAYO
一.LVC_S_LAYO为表格常用的布局属性,包括网格线,宽度自适应,隐藏主键等 二.我们来对比使用前和使用后的表格,这个原始布局风格的表格 三.这个是设置了相关属性的表格
- 146-PHP 使用<<<和HTML混编(二)
<?php $html=<<<HTM1 <title>PHP输出HTML代码</title> <body> <a href=#> ...