leetcode147median-of-two-sorted-arrays
题目描述
are two sorted arrays A and B 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)).
输入
[],[1]
/归并数组,但是不需要完全估计,只需要合并到中位数即可
//但是,如果m+n为偶数,那么返回中间两个值的平均数
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int mid=(m+n)/2+1;
int a=0;
int b=0;
int result;
int result2=0;
for(int i=0;i<mid;i++){
//复杂度为(m+n)/2+1,也就是m+n
if (b>=n || (a<m && A[a]<B[b])){
result=result2;
result2=A[a];
a++;
}
else {
result=result2;
result2=B[b];
b++;
}
}
if ((m+n)%2==0) return double(result+result2)/2.0;
return result2;
}
};
/*
** 获取两个数组的中位数
*/
public double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;//数组A的长度
int n = B.length;//数组B的长度
int l = (m+n+1)/2;
int r = (m+n+2)/2;
/*取两个数的平均值,即适用于总长度m+n是奇数的情况,也适用于是偶数的情况。
* 奇数时,两次调用所获得的值相等;
偶数时,两次调用所获得的值不等。中位数即为两个值的平均值*/
return (getKth(A,0,B,0,l)+getKth(A,0,B,0,r))/2.0;
}
/*获取数组A和数组B结合后的第k小元素(即中位数)
* s1:数组A当前的开始下标
* s2:数组B当前的开始下标
*/
private int getKth(int[] A, int s1, int[] B, int s2, int k){
if(A.length==0){//1.数组A为空,返回数组B的中位数
return B[s2+k-1];
}
if(B.length==0){//2.数组B为空,返回数组A的中位数
return A[s1+k-1];
}
if(k==1){
return Math.min(A[s1],B[s2]);
}
//4.A和B都有多个元素
/*在数组A中找到第k/2小元素a,在数组B中找到第k/2小元素b,
**1)如果a和b相等,那么第k小元素就是a或者b了,
**2)如果a小于b,那么总体的第k小元素不可能在a的第k/2小元素之前,那么就可以将其舍弃了
**3)反之如果a大于b,也就可以舍弃b的第k/2小之前的元素了。*/
int mida = Integer.MAX_VALUE;
int midb = Integer.MAX_VALUE;
if(s1+k/2-1<A.length){
mida = A[s1+k/2-1];
}
if(s2+k/2-1<B.length){
midb = B[s2+k/2-1];
}
if(mida<midb){//去除A中小的部分,继续递归寻找
return getKth(A,s1+k/2,B,s2,k-k/2);
}else{//即mina>minb 去除B中小的部分,继续递归寻找
return getKth(A,s1,B,s2+k/2,k-k/2);
}
}
class
Solution:
def
findMedianSortedArrays(
self
, A , B ):
# write code here
arr
=
sorted
(A
+
B)
if
len
(arr)
%
2
=
=
1
:
return
(arr[(
len
(arr)
/
2
)]
*
1.0
)
if
len
(arr)
%
2
=
=
0
:
return
((arr[(
len
(arr)
/
2
)]
+
arr[((
len
(arr)
/
2
)
-
1
)])
/
2.0
)
leetcode147median-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 ...
- [LeetCode] 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】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 s ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode-【hard】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 ...
- Leetcode4:Median of Two Sorted Arrays@Python
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- Leetcode 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 ...
- [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 ...
随机推荐
- 【题解】CF375D Tree and Queries
Link \(\text{Solution:}\) 讲实话这题有点烦,不知道为啥改了下\(\text{dfs}\)就过了--原版本\(dfs\)好像没啥错啊-- 其实对于子树问题,我们求出原来树的\( ...
- .NET Standard 类库的使用技巧
系列目录 [已更新最新开发文章,点击查看详细] 在前一篇博客<.NET Standard中配置TargetFrameworks输出多版本类库>中详细介绍了如何创建.配置.条件编译. ...
- Python:MySQL数据库环境相关问题
系统环境 Ubuntu 16.04.2 LTS mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using EditLine wrapper P ...
- 修改LiveChart的提示显示位置
问题:修改LiveChart的提示显示位置 摘要:相信WPF开发者在用LiveCharts的时候会有遇到这个需求.就是产品要求折线图的提示要显示的正常点. 需求:如下图所示.原本显示是在下方并且没有小 ...
- 添加Google网络地图功能
在MeteoInfo中添加了Google网络地图功能.打开MeteoInfo软件,选中图层管理区的一个Map Frame(New Map Frame),点击鼠标右键,在弹出菜单中点击Add Web L ...
- MeteoInfoLab脚本示例:TRMM 3B43 HDF数据
TRMM 3B43是卫星观测月平均降水量产品,是HDF的格点数据.需要注意的是数据中降水变量维的顺序里经度维在前纬度维在后,这与通常的设置(纬度维在前经度维在后)相反,需要对获取的二维数组进行转置,使 ...
- day57 Pyhton 前端Jquery09
内容回顾: - 筛选选择器 $('li:eq(1)') 查找匹配的元素 $('li:first') $('li:last') - 属性选择器 - 筛选的方法 - find() 查找后代的元素 - ...
- JDBC的学习(一)
JDBC的学习(一) 概念 所谓英文简写的意思是:Java DataBase Connectivity ,即 Java数据库的连接,用Java语言来操作数据库 本质 简单的来说,就是写这个JDBC的公 ...
- utf-8和utf-8-sig的区别
前言:在写入csv文件中,出现了乱码的问题. 解决:utf-8 改为utf-8-sig 区别如下: 1."utf-8" 是以字节为编码单元,它的字节顺序在所有系统中都是一样的,没有 ...
- 【Azure Redis 缓存 Azure Cache For Redis】Redis性能问题,发现Server Load非常的高,导致正常连接/操作不成功
问题描述 在正常使用Azure Redis的服务中,突然发现Redis 的CPU达到了100%, 正常的使用中发现性能问题严重.从Redis的门户图表中,观察到CPU, Connection,Lent ...