题意:

  给两个有序(升or降)的数组,求两个数组合并之后的中位数。

思路:

  按照找第k大的思想,很巧妙。将问题的规模降低,对于每个子问题,k的规模至少减半。 考虑其中一个子问题,在两个有序数组中找第k大,我们的目的只是将k的规模减半而已,所以可以对比nums1[k/2]和nums2[k/2]的大小,假设nums1[k/2]<=nums2[k/2],那么nums1[0~k/2]这部分必定在0~k之中,那么将这部分删去,k减半,再递归处理。这里面可能有一些细节问题要考虑:

  1. 如果其中1个数组的大小不及k/2。

  2. 如果k=1了,两个数组依然还有元素,那么nums1[0]和nums2[0]必有一个为第k大。

  3. 如果数组大小n+m是奇数还是偶数

  4. 每个子问题的n大还是m大。

  复杂度:k规模每次要么减半,要么就是其中一个数组不够k/2,那么递归到下一次就O(1)解决了。所以复杂度O( logk ),而k=(n+m)/2,所以O( log(n+m) )。

 class Solution {
public: typedef vector<int>::iterator it; int findKth(it seq1,int size1,it seq2,int size2,int k)
{
if(size1==) return seq2[k-];
if(size1>size2) return findKth(seq2,size2,seq1,size1,k);
if(k==) return min(*seq1,*seq2); int p1=min(size1,k/); //保证p1<=p2
int p2=k-p1; if(seq1[p1-]<seq2[p2-])
return findKth(seq1+p1,size1-p1, seq2,size2, k-p1);
else
return findKth(seq1,size1, seq2+p2,size2-p2, k-p2);
} double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n=nums1.size(), m=nums2.size();
if((n+m)&)
return findKth(nums1.begin(),n, nums2.begin(),m, (n+m+)/);
else
{
double a=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/);
double b=findKth(nums1.begin(),n, nums2.begin(),m, (n+m)/+);
return (a+b)/;
}
}
};

AC代码

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 4 : Median of Two Sorted Arrays 找出两个数组的中位数

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

  3. Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】

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

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

  5. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  6. C++ Leetcode Median of Two Sorted Arrays

    坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...

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

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

  8. Leetcode: Median of Two Sorted Arrays. java.

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

  9. LeetCode——Median of Two Sorted Arrays

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

随机推荐

  1. C#入门篇6-10:字符串操作 DateTime操作

    #region DateTime操作 public class C3 { //DateTime常用的操作 public static void Fun1() { //格式:2012-8-16 11:2 ...

  2. JS访问剪切板中的图片

    google出来一个html2canvas,它利用canvas来渲染读取的DOM树,也就是说它只能截取document里的内容,如果要像qq截图那样,应该怎么做?用过百度的Ueditor编辑器的朋友都 ...

  3. ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()'问题

    网上的解释是:“ you need to add -lstdc++, or use 'g++' rather than 'gcc' as your driver program.”,也就是说如果想要使 ...

  4. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  5. [开发笔记]-控制Windows Service服务运行

    用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...

  6. BeanUtils组件

    引入jar包(需要引入依赖的日志jar包) Person p = new Person(); p.setName("Daisy"); p.setAge(12); //对象的copy ...

  7. getBoundingClientRect() 来获取页面元素的位置

    getBoundingClientRect() 来获取页面元素的位置   document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...

  8. C语言基础--switch

    switch格式: switch (条件表达式) { case 整数: // case可以有一个或多个 语句; break; case 整数: 语句; break; default: 语句; brea ...

  9. ctime、atime

    Linux系统文件有三个主要的时间属性,分别是ctime(change time, 而不是create time), atime(access time), mtime(modify time).后来 ...

  10. java 基本类库包的作用

    tools.jar:工具类库,它跟我们程序中用到的基础类库没有关系. Jre库包含的jar文件(jdk1.6):resources.jar.rt.jar.jsse.jar.jce.jar.charse ...