要求:Median of Two Sorted Arrays (求两个排序数组的中位数)

分析:1. 两个数组含有的数字总数为偶数奇数两种情况。2. 有数组可能为

解决方法:

1.排序法

时间复杂度O(m+n),空间复杂度 O(m+n)

归并排序到一个新的数组,求出中位数。

代码:

class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int *C = new int[m+n];
int id1, id2, id3;
id1 = id2 = id3 = 0;
while(id1 < m && id2 < n) {
while(id1 < m && id2 < n && A[id1] <= B[id2]) C[id3++] = A[id1++];
while(id1 < m && id2 < n && B[id2] <= A[id1]) C[id3++] = B[id2++];
}
while(id1 < m) C[id3++] = A[id1++];
while(id2 < n) C[id3++] = B[id2++];
if(id3 & 0x1) {
id1 = C[id3>>1];
delete[] C;
return (double)id1;
}
else {
id1 = C[id3>>1];
id2 = C[(id3>>1)-1];
delete[] C;
return ((double)id1 + (double)id2) / 2.0;
}
}
};

2.使用两个指针查找

时间复杂度O((m+n)/2),空间复杂度O(1)

数组 A ,B 分别使用一个指针,都从头或者从尾部开始走 (m+n) /2(m+n & 0x ==1 时) 步,找出中位数。

代码如下:

 class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
if(m == ) return findMediaArray(B, n);
if(n == ) return findMediaArray(A, m);
unsigned id1, id2;
id1 = id2 = ;
if((m + n) & 0x1)
{
unsigned med = ;
while(id1 < m && id2 < n)
{
while(id1 < m && id2 < n && A[id1] <= B[id2])
{
++id1;
++med;
if(med == (m + n + ) >> ) return (double)A[id1 - ];
}
while(id1 < m && id2 < n && B[id2] <= A[id1])
{
++id2;
++med;
if(med == (m + n + ) >> ) return (double)B[id2 - ];
}
}
while(id2 < n)
{
++med;
if(med == (m + n + ) >> ) return (double)B[id2];
++id2;
}
while(id1 < m)
{
++med;
if(med == (m + n + ) >> ) return (double)A[id1];
++id1;
}
}
else
{
unsigned cnt = ;
int med1 = , med2 = ;
while(id1 < m && id2 < n)
{
while(id1 < m && id2 < n && A[id1] <= B[id2])
{
++id1;
++cnt;
if(cnt == (m + n) >> ) med1 = A[id1 - ];
if(cnt == ((m + n) >> ) + )
{
med2 = A[id1 - ];
return ((double)med1 +(double)med2) / ;
}
}
while(id1 < m && id2 < n && B[id2] <= A[id1])
{
++id2;
++cnt;
if(cnt == ((m + n) >> )) med1 = B[id2 - ];
if(cnt == ((m + n) >> ) + )
{
med2 = B[id2 - ];
return ((double)med1 +(double)med2) / ;
}
}
}
while(id2 < n)
{
++cnt;
if(cnt == (m + n) >> ) med1 = B[id2];
if(cnt == ((m + n) >> ) + )
{
med2 = B[id2];
return ((double)med1 +(double)med2) / ;
}
++id2;
}
while(id1 < m)
{
++cnt;
if(cnt == (m + n) >> ) med1 = A[id1];
if(cnt == ((m + n) >> ) + )
{
med2 = A[id1];
return ((double)med1 +(double)med2) / ;
}
++id1;
}
}
}
double findMediaArray(int A[], int m){
if(m == ) return 0.0;
if(m & 0x1) return (double)A[(m - ) >> ];
else return ((double)A[m >> ] + (double)A[(m >> ) - ]) / ;
} };

code

3.类二分查找
时间复杂度O(lg((m+n)/2)) ~ O(lg(m+n)),空间复杂度O(1)

将问题化解为:查找两个数组中从小到大第 K 个元素。(从大到小亦可)以下为求解过程:

步骤:假定数组 A 中元素个数 m 小于数组 B 中元素个数 n 。从数组A中取出第 min(K/2,m)=pa 个元素,从数组B中取出第 K-pa = pb 个元素,若:

a. A[pa] < B[pb],则将问题化为取数组 A+pa,与数组 B 中第 K - pb 个元素。

b. A[pa] = B[pb], 则第 k 个元素就是 A[pa] 或者 B[pb]。

c. A[pa] > B[pb],则将问题化为取数组 A,与数组 B+pb 中第 K - pa 个元素。

代码:

double findKth(int A[], int m, int B[], int n, int k){
if(m > n) return findKth(B, n, A, m, k);
if(m == 0) return B[k-1];
if(k == 1) return min(A[0], B[0]);
int pa = min(k / 2, m), pb = k - pa;
if(A[pa - 1] < B[pb - 1]){
return findKth(A + pa, m - pa, B, n, k - pa);
}else if(A[pa - 1] > B[pb - 1]){
return findKth(A, m, B + pb, n - pb, k - pb);
}else{
return A[pa - 1];
} }
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int total = m + n;
if(total == 0) return 0.0;
if(total & 0x1){
return findKth(A, m, B, n, (total + 1) >> 1);
}else{
return (findKth(A, m, B, n, total >> 1) + findKth(A, m, B, n, (total >> 1) + 1)) / 2.0;
}
}
};

2.Median of Two Sorted Arrays (两个排序数组的中位数)的更多相关文章

  1. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

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

  3. [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 two ...

  4. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

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

  5. 004 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 ...

  6. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

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

  7. 【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 ...

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

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

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. Winform TreeList递归绑定树节点

    /// <summary> /// 绑定树目录 /// </summary> /// <param name="parentId">父ID< ...

  2. 【转载】VMware虚拟机修改硬盘容量大小

    很多人在安装虚拟机系统的时候,为了节省硬盘空间,把硬盘容量设置得较小,可是后来发现硬盘容量不够用了.在VMware中又不能直接修改虚拟机的硬盘容量大小,或者重建虚拟机系统,非常麻烦. 其实在VMwar ...

  3. log4j.properties配置详解(转)

    本篇文章转自http://it.oyksoft.com/log4j/ 非常感谢原创作者的辛勤编写与分享. 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders ...

  4. 从零开始学习Node.js例子六 EventEmitter发送和接收事件

    pulser.js /* EventEmitter发送和接收事件 HTTPServer和HTTPClient类,它们都继承自EventEmitter EventEmitter被定义在Node的事件(e ...

  5. PHP 调试

    $log_path = '/data0/www/html/hejungw/static/log/uploadlog/'; $log = $log_path . date('Y-m-d') . '.lo ...

  6. Caffe 源碼閱讀(三) caffe.cpp

    补:主要函数运行顺序: main>>GetBrewFunction>>train>>Solve 從main函數說起: 1.gflags庫中爲main函數設置usag ...

  7. iOS运行时与method swizzling

    C语言是静态语言,它的工作方式是通过函数调用,这样在编译时我们就已经确定程序如何运行的.而Objective-C是动态语言,它并非通过调用类的方 法来执行功能,而是给对象发送消息,对象在接收到消息之后 ...

  8. Linux线程-pthread_kill

    该函数可以用于向指定的线程发送信号: int pthread_kill(pthread_t threadId,int signal); 如果线程内不对信号进行处理,则调用默认的处理程式,如SIGQUI ...

  9. Centos Python2 升级到Python3

    1. 从Python官网到获取Python3的包, 切换到目录/usr/local/src #wget https://www.python.org/ftp/python/3.5.1/Python-3 ...

  10. 腾讯优测优分享 | 这些年,我们追过的 fiddler

    腾讯优测是专业的移动云测试平台,提供全面兼容性测试,远程真机租用,漏洞分析等多维度的测试服务,旗下优分享提供大量的移动研发及测试相关的干货! 一.fiddler原理简介 fiddler是目前最强大最好 ...