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 ...
随机推荐
- Flink深入浅出: 应用部署与原理图解(v1.11)
往期推荐: Flink深入浅出:内存模型 Flink深入浅出:JDBC Source从理论到实战 Flink深入浅出:Sql Gateway源码分析 Flink深入浅出:JDBC Connector源 ...
- TP5调用小程序微信支付,回调,在待支付中再次调用微信支付
1,必须要有 $mch_id $key $appid这三个值,是需要去申请的,我是直接用公司的2,购买商品订单号用户openid统一下单名称商品价格(必须以分为单位,调起微信支付)服务器的ip地址(没 ...
- day60 Pyhton 框架Django 03
day61 内容回顾 1.安装 1. 命令行: pip install django==1.11.18 pip install django==1.11.18 -i 源 2. pycharm sett ...
- centos8用firewalld搭建防火墙
一,firewalld的systemd管理命令 启动:systemctl start firewalld 关闭:systemctl stop firewalld 查看状态:systemctl stat ...
- spring boot:使用caffeine+redis做二级缓存(spring boot 2.3.1)
一,为什么要使用二级缓存? 我们通常会使用caffeine做本地缓存(或者叫做进程内缓存), 它的优点是速度快,操作方便,缺点是不方便管理,不方便扩展 而通常会使用redis作为分布式缓存, 它的优点 ...
- docker安装部署neo4j
docker部署neo4j 环境:ubuntu16.04LTS docker安装 详见:菜鸟教程(docker安装) docker国内镜像源配置 第一步,进入阿里云,登陆后点击左侧的镜像加速,生成自己 ...
- 第六章 类(Class) 和对象(Object)
一.笔记导图 二.实例代码: public class PrintCarStatus{ public static void main(String[] args){ int speed; Strin ...
- 手撸了一个HTTP框架:支持Sprng MVC、IOC、AOP,拦截器,配置文件读取...
https://github.com/Snailclimb/jsoncat :仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 距离上一次给小伙伴们汇报简易 ...
- C#一行代码实现(06)跨线程获取控件值,结合BeginInvoke和EndInvoke使用,以DataGridView为例
主要功能 跨线程获取控件值,以DataGridView为例,结合BeginInvoke和EndInvoke使用 一行代码 object cellValue = dataGridView.ExInvok ...
- 【总结】springcloud
一.spirngcloud概述 1.springcloud是什么? spring cloud是一个一站式的开发分布式系统的框架,为开发人员提供了快速构建分布式系统中一些常见模式的工具(如:配置管理,服 ...