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 ...
随机推荐
- Tensorflow学习笔记No.3
使用tf.data加载数据 tf.data是tensorflow2.0中加入的数据加载模块,是一个非常便捷的处理数据的模块. 这里简单介绍一些tf.data的使用方法. 1.加载tensorflow中 ...
- 手把手教你安装TensorFlow2 GPU 版本
参考博客:https://blog.csdn.net/weixin_44170512/article/details/103990592 (本文中部分内容引自参考博客,请大家支持原作者!) 感谢大佬的 ...
- [学习笔记] 数位DP的dfs写法
跟着洛谷日报走,算法习题全都有! 嗯,没错,这次我也是看了洛谷日报的第84期才学会这种算法的,也感谢Mathison大佬,素不相识,却写了一长篇文章来帮助我学习这个算法. 算法思路: 感觉dfs版的数 ...
- SHOI 2014 【概率充电器】
加油,两道了,也就还剩那么二十来道吧,慢慢做...... 题目大意: 给你一颗树,树上的每一个节点都有一定的概率p[i]能冲上电,有电的点,可以通过树上的边,一定概率地将电传递到与它相邻的点,同时对于 ...
- Elasticsearch(4):映射
ES中的映射(mapping)是用于定义索引中文档以及文档中的字段如何被存储和索引(动词)的一种机制,例如,通过映射我们可以进行如下的这些定义: 索引文档中,哪些字符型字段应该被当做全文本类型: ...
- 【贪心算法】HDU 5747 Aaronson
题目大意 vjudge链接 给你一个n,m,求解满足等式x0+2x1+4x2+...+2mxm=n的x0~xm的最小和(xi为非负整数) 数据范围 0≤n,m≤109 思路 n和m都在int范围内,所 ...
- docker-compose编写示例
docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- CentOS8平台nginx日志的定时切分
一,编写bash脚本: [root@yjweb crontab]# vi split_nginx_logs.sh 代码: #!/bin/bash # 备份nginx的日志 # 昨天的日期 file_d ...
- property和setter装饰器
# property装饰器 # 作用: 将一个get方法转换为对象的属性. 就是 调用方法改为调用对象 # 使用条件: 必须和属性名一样 # setter方法的装饰器: # 作用:将一个set方法转换 ...
- spring-boot-route(十九)spring-boot-admin监控服务
SpringBootAdmin不是Spring官方提供的模块,它包含了Client和Server两部分.server部分提供了用户管理界面,client即为被监控的服务.client需要注册到serv ...