要求: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. UOJ Test Round 1

    第一题: 题目大意: 给出N个字符串,字符串的前面部分都是字母且都是一样的,后面部分是数字,按照后面的数字排序.N<=10000 解题过程: 1.第一题是真良心,一开始的做法是把后面的数字分离出 ...

  2. hdu 2059

    ps:终于解决了....卡了我好久.最后用了DP.然后还有记忆化搜索优化了一下.终于AC了 思路:要计算dp[n](就是到第n个站的最短时间,也就是最优方案),必须知道dp[0]到dp[n-1] 设j ...

  3. UE4 - C++ 射线捕捉

    #include "Runtime/Engine/Classes/Kismet/KismetMathLibrary.h" //省略大部分代码 void AMyFPS_Charact ...

  4. swift项目中使用OC/C的方法

    假如有个OC类OCViewController : UIViewController类里有两个方法 //swift调用oc或c的混编是比较常用的,反过来的调用很少.这里只写了swift调用oc和c的方 ...

  5. DedeCMS的分页标签pagelist支持中英文的修改方法[转]

    不得不感叹DedeCMS的强大,可定制性和扩展性太强了,就算不懂php也可以很轻松的修改源码. 今天碰到个新问题,用DedeCMS搞了个中英双语的网站,其他的都修改好了,上线之前发现分页条中英文版中都 ...

  6. 12-16php测试题

    2. 以下哪个SQL语句是正确的( d )A:insert into users ('p001','张三','男'); B:create table (Code int primary key); C ...

  7. C语言处理xml文件的库

    读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好. TinyXML是一个开源的解 ...

  8. Model1

    jsp+javabean的开发模式 此处JavaBean也可是封装的业务逻辑 流程: 浏览器端访问jsp,jsp交给Javabean处理,javabean处理后台数据,交还给Jsp

  9. iOS 的基本框架

    在iOS中框架是一个目录,包含了共享资源库,用于访问该资源库中储存的代码的头文件,以及图像.声音文件等其他资源.共享资源库定义应用程序可以调用的函数和方法.    iOS为应用程序开发提供了许多可使用 ...

  10. js中的this指针(三)

    当一个函数并非一个对象的忏悔时,它会被当作一个函数来调用. 此时,函数中的 this 指针被绑定到了全局对象. 后果:方法不能利用内部函数来帮助工作,由于 this 被绑定了错误的值,将无法共享该方法 ...