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

2017/2/5更新:如果一定要每次扔一半,使得时间复杂度为O(log(m+n))。可以在第一个数组找前k1个,第二个数组找前k2个,使得k1+k2 == k, 分情况:

1. if A[k1] < B[k2], then A[k1]及之前的、B[k2+1]及之后的都不可能成为第k个元素,所以扔掉

2. else if A[k1] > B[k2], then A[k1+1]及之后的、 B[k2]及之前的都不可能成为第k个元素。扔掉

if (aMid < bMid) Keep [aRight + bLeft]
else Keep [bRight + aLeft]
 1 public class Solution {
 2     public double findMedianSortedArrays(int A[], int B[]) {
 3         if((A.length+B.length)%2==1)
 4             return helper(A,B,0,A.length-1,0,B.length-1,(A.length+B.length)/2+1);
 5         else
 6             return (helper(A,B,0,A.length-1,0,B.length-1,(A.length+B.length)/2)
 7                    +helper(A,B,0,A.length-1,0,B.length-1,(A.length+B.length)/2+1))/2.0;
 8     }
 9
10     private int helper(int A[], int B[], int i, int i2, int j, int j2, int k)
11     {
12         int m = i2-i+1;
13         int n = j2-j+1;
14         if(m>n)
15             return helper(B,A,j,j2,i,i2,k);
16         if(m==0)
17             return B[j+k-1]; //这是相对距离,不是相对于0,而是这轮起点j,所以决定了25行27行要减posA或posB
18         if(k==1)
19             return Math.min(A[i],B[j]);
20         int posA = Math.min(k/2,m);
21         int posB = Math.min(k-posA, n);
22
23         if(A[i+posA-1]<B[j+posB-1])
24             return helper(A,B,i+posA,i2,j,j+posB-1,k-posA);
25         else
26             return helper(A,B,i,i+posA-1,j+posB,j2,k-posB);
27     }
28 }

转载于:https://www.cnblogs.com/EdwardLiu/p/3982815.html

Leetcode: Median of Two Sorted Arrays相关推荐

  1. LeetCode: Median of Two Sorted Arrays 解题报告

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  2. [leetcode]Median of Two Sorted Arrays @ Python

    原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...

  3. LeetCode Median of Two Sorted Arrays (DFS)

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  4. C语言求一个数组中第k大的数,leetcode | Median of Two Sorted Arrays 寻找2个有序数组中第k大的值...

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

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

  6. 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数

    题目:LeetCode 004 Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m an ...

  7. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 1 import java.util.* 2 3 class Solutio ...

  8. LeetCode刷题第二天——3Longest Substring Without repeating character 4 Median of Two Sorted Arrays...

    混淆点: 子串 连续 子序列 可以不连续 知识点: HashMap: 出现问题: 1.使用unordered_map头文件时报错 #error This file requires compiler ...

  9. Leetcode平台上的Median of Two Sorted Arrays题目用Java快排实现

    Leetcode平台上的Median of Two Sorted Arrays题目,大意就是找两个已排序数组的中位数.今天先用快排的方式实现一下,代码如下: There are two sorted ...

最新文章

  1. 网络系统工程师的最终归宿(一)创业还是打工
  2. nginx http子模块conf的初始化
  3. Python 使用 Scrapy 发送 post 请求的坑
  4. MySQL只有80端口开放_Centos 开放80端口
  5. windows服务器上安装的有mysql环境怎么创建数据库_Windows环境MySQL数据库安装方法...
  6. 计算机视觉未有深度学习之前
  7. mongoDB的配置以及运行
  8. 用脆弱性评估流程击败黑客
  9. CentOS安装JDK 17
  10. 排列图 - QCC小组活动推行知识系列介绍(三)
  11. android GitLab使用教程
  12. 百度AI开放平台文字之身份证识别的实现
  13. UE4资源————Pak
  14. 关于wmsys.wm_concat() 多条记录合并,及可能出现的异常ORA-31061:XDB错误:special char to escaped char conversion failed
  15. OFDMA和OFDM的区别
  16. 子域名收集 -- Esd
  17. ignore在mysql中什么意思_INSERTIGNORE与INSERTINTO的区别_MySQL
  18. 一键关闭windows防火墙_win7系统怎么一键关闭危险端口怎么办【操作方法】
  19. 2012年5月编程语言排行榜:C再次位居第一 Java 占有率持续下降
  20. C语言教程:十进制转换任意进制

热门文章

  1. opencv python 多帧降噪算法_OpenCV-Python中用于视频跟踪的Meanshift和Camshift算法介绍...
  2. 剑指 Offer 51-----59
  3. pycharm remote 远程项目 同步 本地_利器:PyCharm本地连接服务器搭建深度学习实验环境的三重境界...
  4. 嵌入式C语言编程——.h文件与.c文件
  5. 计算机语言 angela,Angela
  6. Terminal中输入一行命令快速移动光标至行首行尾
  7. 光纤收发器的工作原理以及使用方法
  8. 光模块的参数有哪些呢?
  9. [渝粤教育] 盐城工学院 水处理微生物学 参考 资料
  10. 【渝粤教育】国家开放大学2019年春季 0691-22T物理化学及实验 参考试题