There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

将两个有序数组合并,注意题目求得是the median of the two sorted array,

当m+n是奇数时返回的是合并后的中间数即C[(m+n)/2]

当m+n是偶数时返回的是合并后的中间两个数的平均数即(C[(m+n)/2]+C[(m+n)/2-1]+0.0)/2

注意题目求的是double,空间复杂度是O(m+n)

#include <iostream>
#include <vector>
#include <algorithm> using namespace std; double findMedianSortedArrays(int A[], int m, int B[],int n){
int *C = new int[m+n],i=,j=,k = ;
while(i < m && j < n ){
if(A[i] > B[j]) C[k++]=B[j++];
else if(A[i] < B[j]) C[k++] = A[i++];
else { C[k++]=A[i++];C[k++] = B[j++];}
}
if(i < m ){
for(int idx = i ; idx < m ; idx++) C[k++] = A[idx];
}
if( j< n){
for(int idx = j; idx < n; idx++) C[k++] = B[idx];
}
double mid = double(C[(m+n)/]);
if((m+n)% == ) mid = (C[(m+n)/]+C[(m+n)/-]+0.0)/;
delete [] C;
return mid;
} int main(){
int A[] ={,} ;
int B[] = {};
cout<<findMedianSortedArrays(A,,B,)<<endl;
}

利用二分查找,时间复杂度O(log(m+n))

#include <iostream>
#include <algorithm> using namespace std; double findKth(int A[], int m, int B[], int n , int k){
if(m > n){
return findKth(B,n,A,m,k);
}else{
if( m == ) return B[k-];
if( k == ) return min(A[],B[]);
int first = min(k/,m),second = k - first;
if( A[first- ] < B[second-])
return findKth(A+first,m-first,B,n,k-first);
else if(A[first-] > B[second-])
return findKth(A,m,B+second,n-second,k-second);
else return A[first-];
}
} double findMedianSortedArrays(int A[], int m, int B[], int n){
int total = m + n;
if(total & 0x1)
return findKth(A,m,B,n,(total>>)+);
else
return (findKth(A,m,B,n,(total>>)) + findKth(A,m,B,n,(total>>)+))/;
} int main(){
int A[] ={,,} ;
int B[] = {,,};
cout<<findMedianSortedArrays(A,,B,)<<endl;
}

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] 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]Median of Two Sorted Arrays @ Python

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

  4. LeetCode—— Median of Two Sorted Arrays

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

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

  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

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

  8. leetcode:Median of Two Sorted Arrays分析和实现

    这个问题的大意是提供两个有序的整数数组A与B,A与B并集的中间数.[1,3]与[2]的中间数为2,因为2能将A与B交集均分.而[1,3]与[2,4]的中间数为2.5,取2与3的平均值.故偶数数目的中间 ...

  9. LeetCode Median of Two Sorted Arrays 找中位数(技巧)

    题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...

随机推荐

  1. jQuery – 8.事件和事件参数

        事件 (*)JQuery中的事件绑定:$("#btn").bind("click",function(){}),每次都这么调用太麻烦,所以jQuery可 ...

  2. XAML语言介绍

    <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...

  3. C#关键字ref和out

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Di ...

  4. HDU5792 World is Exploding(树状数组)

    一共6种情况,a < b且Aa < Ab, c < d 且Ac > Ad,这两种情况数量相乘,再减去a = c, a = d, b = c, b = d这四种情况,使用树状数组 ...

  5. phpcms v9最常用的22个调用代码

    新源网络工作室友情总结phpcms v9最常用的22个调用代码: 调用最新文章,带所在版块{pc:get sql="SELECT a.title, a.catid, b.catid, b.c ...

  6. C#中的Infinity有个小坑

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 昨天家里有事,上网也不方便,就没有推送文章.今天很累,也不长篇大论了.简单介绍一下最近遇到的 ...

  7. OpenMesh 删除网格顶点

    OpenMesh 提供了 delete_vertex() 函数来实现从网格中删除顶点,在删除掉顶点的同时,所有与该顶点相连的边也同时被删除. OpenMesh 官方文档 中给的顶点删除函数声明如下: ...

  8. javascript中的true和false

    今天遇到一个问题,执行下面的代码返回true还是false?请说明理由 console.log([] == ![]) 在浏览器中运行了一下,发现结果是true.为什么会这样呢?于是查找了相关的资料. ...

  9. SQL..如何用命令删除数据库中所有的表?

    要删除所有的用户表: declare @sql varchar(8000) SELECT @sql='drop table ' + name FROM sysobjects WHERE (type = ...

  10. Intel CPU MMX SSE SSE2/3/4指令集手册下载URL

    在线查看的网址: https://software.intel.com/sites/landingpage/IntrinsicsGuide/ Intel® 64 and IA-32 Architect ...