http://oj.leetcode.com/problems/median-of-two-sorted-arrays/

找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想到了使用二分,但具体怎么做,没分析清楚。

参考了网上http://blog.csdn.net/zxzxy1988/article/details/8587244 ,其他的类似找两个有序数组中第 k 大的也可以这样处理。

#include <iostream>
using namespace std; double findKth(int a[],int m,int b[],int n,int k)
{
if(m>n) //总是让a中个数小于b中个数
return findKth(b,n,a,m,k);
if(m == ) //一个数组为空了,只需要在b中求第k个
return b[k-];
if(k == ) //就差一个数了
return min(a[],b[]);
int pa = min(k/,m), pb = k - pa; //好写法
if(a[pa - ]<b[pb-]) //a中的pa个可以都算进来了
return findKth(a+pa,m-pa,b,n,k-pa); //a+pa,m-pa来表示,good
else if(a[pa-]>b[pb-])
return findKth(a,m,b+pb,n-pb,k-pb);
else //如果相等
return a[pa - ];
}
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total = m + n;
if(total & 0x1) //如果一共有奇数个
return findKth(A,m,B,n,total/+);
else //中位数 如果有偶数个数据,那么就是中间两个数字的平均数
return (findKth(A,m,B,n,total/) + findKth(A,m,B,n,total/+) )/;
}
}; int main()
{
int A[] = {,,,,};
int B[] = {,,,};
Solution *myS = new Solution();
double ans = myS->findMedianSortedArrays(A,,B,);
cout<<ans<<endl;
return ;
}

LeetCode OJ--Median of Two Sorted Arrays ***的更多相关文章

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

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

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

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

  3. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

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

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

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  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 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

  9. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

  10. leetcode 4. Median of Two Sorted Arrays

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

随机推荐

  1. CPL学习笔记(一)

    整型 计算机的内存的基本单位是位(bit),可以将其看作电子开关,可以开,表示1:也可以关表示0. 字节(byte)通常指八位的内存单元. 8bit=1byte=1B; 1KB=1024B; 1M=1 ...

  2. C#基础-字符串

    字符串比较,strA.CompareTo(strB) A大于B 正数 A小于B 负数 A等于B 0 string strA = "ab"; string strB = " ...

  3. 【android】安卓开发apk列表

    - 谷歌的Zxing框架的扫码软件 (目前国内的应用商店很少此种类型的扫码app) - 解析IP地址功能,从IP地址(子网掩码)自动解析出网段,广播地址

  4. gcc——预处理(预编译),编译,汇编,链接

    一,预编译 操作步骤:gcc -E hello.c -o hello.i 主要作用: 处理关于 “#” 的指令 [1]删除#define,展开所有宏定义.例#define portnumber 333 ...

  5. Java线程和多线程(四)——主线程中的异常

    作为Java的开发者,在运行程序的时候会碰到主线程抛异常的情况.如果开发者使用Java的IDE比如Eclipse或者Intellij IDEA的话,可能是不需要直接面对这个问提的,因为IDE会处理运行 ...

  6. python - 接口自动化测试实战 - case1 - 再次优化版

    本次优化: 1.  各级分Package 2.  封装[ReadExcel]类 3.  封装[ReadConfig]类 4.  封装[GetLog]类 5.  引入ddt数据驱动测试,优化测试用例代码 ...

  7. 《百词斩·象形9000》第一册(下) 符号Symbol 1

    001-version n.版本:译文 This is the first version of the software #这是软件开发的第一个版本: 002-element n.成分:要素:元素: ...

  8. 深度学习:Sigmoid函数与损失函数求导

    1.sigmoid函数 ​ sigmoid函数,也就是s型曲线函数,如下: 函数: 导数: ​ 上面是我们常见的形式,虽然知道这样的形式,也知道计算流程,不够感觉并不太直观,下面来分析一下. 1.1 ...

  9. phpcms调用一个指定的栏目

    很多初学者在学习phpcms的时候在模板中都是静态的写导航的url,那样在后期维护的时候可是个巨大的工程啊,所以我在学的时候就想起织梦是能指定调用栏目的url,借鉴了这一想法, {$CATEGORY[ ...

  10. activemq的安装启动

    Activemq安装和启动   官网:http://activemq.apache.org/   安装启动: $ tar -zxvf apache-activemq-5.11.1-bin.tar.gz ...