题目描述

有两个大小分别为m和n的有序数组A和B。请找出这两个数组的中位数。你需要给出时间复杂度在O(log (m+n))以内的算法。

There
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

输入

[],[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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【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 ...

  4. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  5. 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 ...

  6. 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 ...

  7. leetcode 4. Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  8. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  9. 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 ...

  10. [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 ...

随机推荐

  1. 使用HTML的基本结构创建网页

    1.       网页的扩展名--html或htm 2.       如何新建网页? 步骤1: 在电脑的空白处,右键选择-->新建--文本文档 步骤2: 把txt的扩展名,改成html或htm, ...

  2. RocketMQ消息丢失解决方案:事务消息

    前言 上篇文章,王子通过一个小案例和小伙伴们一起分析了一下消息是如何丢失的,但没有提出具体的解决方案. 我们已经知道发生消息丢失的原因大体上分为三个部分: 1.生产者发送消息到MQ这一过程导致消息丢失 ...

  3. MySQL常用操作列表

    DROP DATABASE IF EXISTS flaskweb; CREATE DATABASE flaskweb; USE flaskweb; GRANT ALL PRIVILEGES ON fl ...

  4. 多测师讲解selenium--常用关键字归纳-_高级讲师肖sir

    常见的定位方式: 1.通过id定位 id=kw 2.通过name定位 name=wd 3.通过xpath相对路径定位:xpath=//*[@id="kw"] 4.通过两个属性值定位 ...

  5. 【线段树分治】Dash Speed

    代码的美妙 #include <bits/stdc++.h> %:pragma GCC optimize(3) using namespace std; const int maxn=7e ...

  6. HTML轮播(3)

    前言 现在给轮播加上可视化的点,实际这样的轮播已经算完成的了 CSS #LB { width: 100%; height: 948px; overflow: hidden; position:rela ...

  7. CAS 算法与 Java 原子类

    乐观锁 一般而言,在并发情况下我们必须通过一定的手段来保证数据的准确性,如果没有做好并发控制,就可能导致脏读.幻读和不可重复度等一系列问题.乐观锁是人们为了应付并发问题而提出的一种思想,具体的实现则有 ...

  8. 对于72种常用css3的使用经验

    对于72种常用css3的使用经验 保存网站源码 目的 保证有足够的css和js文件源码 拿到当前网页的整体布局 本地化 创建web项目 将web项目创建出来 在项目中创建一个文件夹 将所有的js和cs ...

  9. 如何按名称或PID查找一个进程?如何按端口号查找一个进程?如何查看一个进程的CPU和内存、文件句柄使用情况?如何查看CPU利用率高的TOP10进程清单?如何根据PID强制终止进程?

    如何按名称或PID查找一个进程?如何按端口号查找一个进程?如何查看一个进程的CPU和内存.文件句柄使用情况?如何查看CPU利用率高的TOP10进程清单? 目录 如何按名称或PID查找一个进程?如何按端 ...

  10. 使用经纬度得到位置Geocorder

    先得到经纬度再用geocorder 显示位置,需要手机打开位置权限,使用GPS的话把注释去掉,GPS在室内很容易收不到信号,得到位置为空 public class MainActivity exten ...