题意:

  给两个有序(升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. java网络编程socket解析

    转载:http://www.blogjava.net/landon/archive/2013/07/02/401137.html Java网络编程精解笔记2:Socket详解 Socket用法详解 在 ...

  2. placeholder在ie789下无效

    <input type="text" class="input" placeholder="用户名/手机号码/邮箱" value=&q ...

  3. NimBus一个好的开发框架

    NimbusKit是一个非常适合有经验的开发人员使用的iOS开发框架,具备完整的文档,并且提供了模块化的方式来解决iOS开发中的各种不同需求.最重要的是,该框架会经常添加一些新的组件和功能. Nimb ...

  4. linux 执行php文件

    /opt/php5/bin/php /home/Xcar/tag/interface/tag_api4hbase.php tag_export2file "/tmp/guo.php" ...

  5. 二分搜索法(转载自vanezkw)

    二分查找算法java实现 今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. ...

  6. 在Linux中查看文件的编码及对文件进行编码转换

    如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...

  7. WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)

    一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...

  8. SQL with PL/SQL

    DDL commands --> create user / table / view / sequence alter DML --> data manipulation languag ...

  9. DotNetBar v12.9.0.0 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.9.0.0 如果遇到破解问题可以与我 ...

  10. 如何获取google可以访问的IP地址

    由于某些原因,google的部分网站无法打开,导致我们的好些资源都无法找到,今天在网上看到一篇文件,教大家如何能找到可以访问的google. 假如我们需要访问的是:https://code.googl ...