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 解析XML的几种方法
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便. XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已. 基本的解析方式 ...
- ActiveMQ的几种集群配置
ActiveMQ是一款功能强大的消息服务器,它支持许多种开发语言,例如Java, C, C++, C#等等.企业级消息服务器无论对服务器稳定性还是速度,要求都很高,而ActiveMQ的分布式集群则能很 ...
- ubuntu 下使用 putty 调试
转自:http://blog.csdn.net/wh_19910525/article/details/39313457 Ubuntu的机子上,插上USB2COM线,准备开工. 检查USB2COM在本 ...
- Pyqt 时时CPU使用情况
借鉴代码来自:https://github.com/hgoldfish/quickpanel 实现代码: # -*- coding:utf-8 -*- from __future__ import p ...
- Sencha Architect 安装与使用
http://www.sencha.com/products/touch/ Sencha SDK Tools Advanced JavaScript and CSS Project Build Too ...
- golang exec Command
package mainimport ( "fmt" "log" "os/exec")func main() { out, err := e ...
- jquery获取和设置元素高度宽度
jquery获取和设置元素高度宽度 1.height()/ width() 取得第一个匹配元素当前计算的高度/宽度值(px) height(val)/ width(val) 为每个匹配的元素设置CSS ...
- 【c++】必须在类初始化列表中初始化的几种情况
转自:http://www.cnblogs.com/kaituorensheng/p/3477630.html 1. 类成员为const类型 2. 类成员为引用类型 #include <iost ...
- The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience
全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...
- 使用python做科学计算
这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...