题目意思很简单将两个有序数组合并之后的中位数找出来。题目要求使用log(m+n)的时间复杂度来做。

虽然言简意赅,但不得不承认这个题目我自己想了好久也没做出来,隐约觉得应该使用寻找第k大数的算法来做,但是具体到这个题目,编码多次都以失败告终,所以不得不去网上参考下别人的思路和代码。

参考链接:http://blog.csdn.net/zxzxy1988/article/details/8587244

但是这个方法现在已经无法在leetcode上AC,需要在两个递归的return处进行修改。

思路是这样的:

我们寻找两个数组的第k个数,那么我们首先找到两个数组中的第(k/2-1)个数。比较两个数组中这个数的大小来进行不同递归。

代码如下:

     int min(int a,int b)
{
return a>b?b:a;
}
double findknumber(int *a,int *b,int al,int bl,int k)
{
if (al > bl)
return findknumber(b, a, bl, al, k);
if (al == )
return b[k - ];
if (k == )
return min(a[], b[]);
int aindex = min(k / , al);
int bindex = k - aindex;
if (a[aindex - ] < b[bindex - ])
return findknumber(a + aindex, b, al - aindex, bindex, k - aindex);
else if (a[aindex - ] > b[bindex - ])
return findknumber(a, b + bindex, aindex, bl - bindex, k - bindex);
else
return a[aindex - ]; }
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int total=nums1Size+nums2Size;
if(total%==)
return findknumber(nums1,nums2,nums1Size,nums2Size,total/+);
else
return (findknumber(nums1,nums2,nums1Size,nums2Size,total/+)
+findknumber(nums1,nums2,nums1Size,nums2Size,total/))/2.0; }

这个代码还存在两个问题,后续我再补充:

存在一个找第k个数的坐标问题;

这个算法的真实时间复杂度如何;

Median of Two Sorted Arrays-分治法的更多相关文章

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

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

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

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

  3. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  4. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  5. 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)

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

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

  7. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

    4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...

  8. LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

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

  10. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. 自增或自减例子:i++和++i的相同点和不同点

    /* Name:++i和i++的区别 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月15日 02:40:27 Description:熟悉前自增或自减的 ...

  2. android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理

    代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...

  3. rdo(remote data objects) repo openstack icehouse

    problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...

  4. SQL Server数据转换【包括Geometry类型】的技巧总结

    1. 字段的组合: update new_master_location set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+con ...

  5. No.1小白的HTML+CSS心得篇

    一个web前端的小白,听前辈说写好笔记很关键,so 特此用博客来开始记录自己的旅程——Web之路 最近几天看的HTML 1.纠正一个认知错误 “HTML是一种编程语言”  ————(错) HTML ( ...

  6. 安卓中onBackPressed ()方法的使用

    一.onBackPressed()方法的解释 这个方法放在 void android.app.Activity.onBackPressed() 在安卓API中它是这样解释的: public void ...

  7. 关于MVC EntityType has no key defined的问题

    关于MVC EntityType has no key defined的问题 在测试MVC时发现一个奇怪的问题,在Model中定义数据类时,如 public class UserInfo { publ ...

  8. 【Tomcat】使用Eclipse发布项目时,项目启动路径错误。

    这种情况下,,通常会去C:\User\[USERNAME]\或者Tomcat路径下面的一个temp文件夹里面找项目文件,由此出现报错. 这时候解决方法为: 双击Eclipse的Servers里面的to ...

  9. handlebar.js使用

    官方网站:http://handlebarsjs.com/ 下载及查看使用帮助,或者用百度cdn引用 一.定义模板 <script id="entry-template" t ...

  10. 浅谈JSP(一)

    一.JSP引言 JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计.它是在传统的网页HTML文件(*.htm,*.html)中插入Java ...