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. 【dp】石子归并

    玄学NPC 题目描述 有一堆石头质量分别为W1,W2,…,Wn.(Wi≤10000),将石头合并为两堆,使两堆质量的差最小. 输入 输入第一行只有一个整数n(1≤n≤50),表示有n堆石子.接下去的n ...

  2. 【模板】有旋Treap

    如题,这是一个模板... #include <algorithm> #include <iostream> #include <cstring> #include ...

  3. 13Shell脚本—编写简单脚本

    1. 概述 Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interrctive): 用户每输入一条命令就立即执行. 批处理(Batch): 由用户事先编写好一个完整的 Shell 脚 ...

  4. GoF23种设计模式之结构型模式之装饰模式

    一.概述 动态地给一个对象添加一些额外的职责.装饰模式比生成子类更为灵活. 二.适用性 1.在不影响其他对象的情况下,以动态.透明的方式给但个对象添加职责. 2.处理那些可以撤销的职责. 3.当不能采 ...

  5. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  6. ASCII码表含义

    在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写)以及0.1等数字还有一些常用的符号(例如*.#. ...

  7. HDU 3394 双连通分量 桥 Railway

    第一个答案是统计图中桥的个数 如果一个点-双连通分量中边的个数大于点的个数那么这个块中所有的边都是冲突的,累加到第二个答案中去. #include <iostream> #include ...

  8. 回调深入理解 同步回调 以android中View.OnClickListener为列

    现在来分析分析下Android View的点击方法onclick();我们知道onclick()是一个回调方法,当用户点击View就执行这个方法,我们用Button来举例好了   //这个是View的 ...

  9. 遍历Request.QueryString

    Request.QueryString 返回的是 NameValueCollection, 而NameValueCollection实现了IEnumerable的GetEnumerator方法,只是G ...

  10. 元类相关(type & metaclass)

    metaclass作用: 1) 拦截类的创建 2) 修改类 3) 返回修改之后的类 """为什么要用metaclass类而不是函数? 由于__metaclass__可以接 ...