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

Have you been asked this question in an interview? 
在求第k小的树时,按两个数组的长度比例分配k

import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;class Main {public static final boolean DEBUG = true;public BufferedReader cin;public PrintWriter cout;public StreamTokenizer tokenizer;public double findMedianSortedArrays(int[] A, int[] B) {int m = A.length;int n = B.length;if (((m + n) & 1) != 0) {return (double) findKth(A, B, (m + n) / 2, 0, m - 1, 0, n - 1);} else {return (findKth(A, B, (m + n) / 2, 0, m - 1, 0, n - 1) + findKth(A,B, (m + n) / 2 - 1, 0, m - 1, 0, n - 1)) * 0.5;}}public int findKth(int[] A, int[] B, int k, int astart, int aend,int bstart, int bend) {int aLen = aend - astart + 1;int bLen = bend - bstart + 1;if (aLen == 0)return B[bstart + k];else if (bLen == 0)return A[astart + k];if (k == 0) {return A[astart] < B[bstart] ? A[astart] : B[bstart];}int amid = (aLen * k) / (aLen + bLen);int bmid = k - amid - 1;amid = astart + amid;bmid = bstart + bmid;if (A[amid] > B[bmid]) {k = k - (bmid - bstart + 1);aend = amid;bstart = bmid + 1;} else {k = k - (amid - astart + 1);astart = amid + 1;bend = bmid;}return findKth(A, B, k, astart, aend, bstart, bend);}public void output(double ans){cout.println(ans);cout.flush();}public void init() {try {if (DEBUG) {cin = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\uva_in.txt")));} else {cin = new BufferedReader(new InputStreamReader(System.in));}cout = new PrintWriter(new OutputStreamWriter(System.out));tokenizer = new StreamTokenizer(cin);} catch (Exception e) {e.printStackTrace();}}public String next() {try {tokenizer.nextToken();if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {return String.valueOf((int) tokenizer.nval);} else {return null;}} catch (Exception e) {e.printStackTrace();return null;}}public static void main(String[] args) {Main solver = new Main();solver.init();int t = Integer.parseInt(solver.next());while (t-- > 0) {int m = Integer.parseInt(solver.next());int[] a = new int[m];for (int i = 0; i < m; i++) {a[i] = Integer.parseInt(solver.next());}int n = Integer.parseInt(solver.next());int[] b = new  int[n];for (int i = 0; i < n; i++) {b[i] = Integer.parseInt(solver.next());}double ans = solver.findMedianSortedArrays(a, b);solver.output(ans);}}
}

LeetCode Median of Two Sorted Arrays (DFS)相关推荐

  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

    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. python中enumerate在for循环中用法_python中enumerate的用法实例解析
  2. Tableau必知必会之如何快速制作 词云(文字云)
  3. 2017计算机考研统考大纲,2017年考研计算机大纲详解(六):计算机网络
  4. 自定义日历控android,Android自定义日历Calender代码实现
  5. ubuntu14.04 开启root登陆
  6. 1011.log4cplus移植记录
  7. css animate属性spend不生效
  8. python __iter____next__
  9. 笨办法学python 3 48题_附录练习 8-10 笨办法学Python3
  10. CompoundButton(checkbox,switch,ToggleButton)和RadioGroup OnCheckedChangeListener() 引用冲突问题
  11. 当前常见游戏服务器引擎
  12. 哈 佛 大 学 图 书 馆 训 言
  13. AI赋能下的出入口控制技术在公共安全建设中的深度应用
  14. 微服务时代之2017年五军之战
  15. excel单元格内的数值向上、向下取整
  16. 基于JAVA师大家教中心管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
  17. 东南大学计算机考研专业课科目,东南大学计算机技术(专业学位)研究生考试科目和考研参考书目...
  18. [08006][17002] io 错误: the network adapter could not establish the connection (connection_id=qdoc1bwg
  19. 【视觉SLAM十四讲】第三讲 三维空间刚体运动
  20. 虚拟机安装与配置<3>---VMware 16.0上调整虚拟电脑的配置

热门文章

  1. QT精彩实例分析第5章-0
  2. python爬虫框架排行榜-Python爬虫框架--pyspider初体验
  3. python在线-python+在线
  4. python字符串find函数-python字符串查找函数的用法详解
  5. python学起来难不难-Python自学难不难,培训班推荐?
  6. python在线读-文档编辑工具Markdown
  7. python之父去面试-前端两年月入30K,高频面试题整理(含答案)
  8. python画柱形图-Python绘制精美图表之双柱形图
  9. 自学python需要买书吗-Python真的适合每个人学习吗 学习Python需要多久
  10. python装什么版本-新手python用什么版本好?