题意:

  给两个有序(升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. 【转】数据库范式(1NF 2NF 3NF BCNF)详解一

    以下内容转自:http://jacki6.iteye.com/blog/774866 --------------------------------------------分割线---------- ...

  2. Windows server2003 + sql server2005 集群配置安装

    http://blog.itpub.net/29500582/viewspace-1249319/

  3. JavaWeb chapter 1 http协议

    1.  静态web和动态web的区别: 静态web和动态web最本质的区别是静态web是无法进行数据库操作,而动态web是可以进行数据库操作的.动态web的最大特点就是具有交互性,所谓交互性就是服务器 ...

  4. 深入理解Redis:命令处理流程

    Redis是著名的NoSQL键值数据库服务器,为了保证效率,其数据都缓存在内存中.与Memcached相比,Redis支持的数据类型更多,包括String,List,Set,Zset和Hash.下面简 ...

  5. iOS 消息推送原理及实现Demo

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...

  6. DatagridView自动充满屏幕,并能指定某列宽度

    1.要使datagridview正好充满屏幕,设置其AutoSizeColumnsMode属性为fill 2. 同时,我们想要某列宽点,某列窄点,在AutoSizeColumnsMode属性为fill ...

  7. C# 正则表达式 验证:数字、带小数点数字、电话和手机

    一.带小数点数字 public static bool IsNumber(string input) { string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\ ...

  8. xml装php数组

    $data = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA); $arr = converArray($data); ...

  9. ssh curl 命令理解

    使用一条命令抓取一本小说 curl "http://www.23hh.com/book/1/1019/"|iconv -c -f gbk -t utf8 |sed 's/" ...

  10. WP8.1 Study5:Data binding数据绑定

    一.数据绑定 最简单的编程UI控件的方法是写自己的数据来获取和设置控件的属性,e.g. , textBox1.Text = "Hello, world"; 但在复杂的应用程序,这样 ...