Leetcode 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 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的更多相关文章
- 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 ...
- [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 ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- 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 ...
- C++ Leetcode Median of Two Sorted Arrays
坚持每天刷一道题的小可爱还没有疯,依旧很可爱! 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. ...
- 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 ...
- 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的平均值.故偶数数目的中间 ...
- LeetCode Median of Two Sorted Arrays 找中位数(技巧)
题意: 给两个有序(升or降)的数组,求两个数组合并之后的中位数. 思路: 按照找第k大的思想,很巧妙.将问题的规模降低,对于每个子问题,k的规模至少减半. 考虑其中一个子问题,在两个有序数组中找第k ...
随机推荐
- Java集合源码学习(五)几种常用集合类的比较
这篇笔记对几个常用的集合实现,从效率,线程安全和应用场景进行综合比较. >>ArrayList.LinkedList与Vector的对比 (1)相同和不同都实现了List接口,使用类似.V ...
- JavaWeb学习之Path总结、ServletContext、ServletResponse、ServletRequest(3)
1.Path总结 1.java项目 1 File file = new File(""); file.getAbsolutePath(); * 使用java命令,输出路径是,当前j ...
- SQLAlchemy高级ORM之改查删除及GROUP,JOIN...
按书上案例来的. #coding=utf-8 from datetime import datetime from sqlalchemy import (MetaData, Table, Column ...
- win7Java开发环境配置
win7下Java开发环境的配置 首先下载符合操作系统版本的jdk,比如最新的jdk8: 下载链接:http://www.oracle.com/technetwork/java/javase/down ...
- HTML5应用之文件拖拽上传
使用HTML5的文件API,可以将操作系统中的文件拖放到浏览器的指定区域,实现文件上传到服务器.本文将结合实例讲解HTML5+jQuery+PHP实现拖拽上传图片的过程,来看下HTML5的魅力吧. H ...
- c++ 的 static_cast
http://www.cnblogs.com/pigerhan/archive/2013/02/26/2933590.html #include "Person.h" #inclu ...
- 发送http请求get方法
//获取网页html NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest ...
- pythonyCool-moviepy
你想要登上山顶去看美丽的风光,却在山腰发现了草莓. 今天给大家推荐一个很酷的python包moviepy.我在伯乐在线发现的看这个链接: http://python.jobbole.com/81185 ...
- 异步框架asyn4j的原理
启动时调用init方法 public void init(){ if (!run){ run = true; //工作队列 workQueue = newPriorityBlockingQueue(m ...
- TOMCAT配置外部应用
原来我们都是把项目放到webapps目录下,但其实是可以把项目放到其他文件夹下的,如果把项目放到其他目录下同时也希望tomcat可以运行它,有两种方法: 第一种方法: conf/server.xm ...